如何使用 spring 集成动态地通过 http 标头传递值 [英] how to pass values through http headers dynamically using spring integration

查看:61
本文介绍了如何使用 spring 集成动态地通过 http 标头传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个休息应用程序,我的休息应用程序总是通过通过 http 标头传递一些参数来调用.我的休息应用程序中有一个过滤器,它为每个请求调用并从 http 标头中检索参数,如图所示下面.

I am working on an rest application, and my rest application is always called by passing some parameters through http headers.And I have a filter in my rest application which gets invoked for every request and retrieves the parameters from http headers as shown below.

 @Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpServletRequest=(HttpServletRequest) request;
    String email = httpServletRequest.getHeader("user-email");         
    String userName = httpServletRequest.getHeader("user-name"); 
    chain.doFilter(request, response);
}

我的rest应用程序使用spring集成依次调用soap服务.调用soap服务的代码如下.

my rest application in turn calls an soap service using spring integration.And the code for calling the soap service is as below.

@RequestMapping(value = "/projects", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Project> getProject(HttpServletRequest httpRequest) {
    GetAuthorizedWebSendTransferProjects request = new GetAuthorizedWebSendTransferProjects();
    GetAuthorizedWebSendTransferProjectsResponse response = gw.getResponse(request);
    JAXBElement<ArrayOfProjectContainer> arr = response.getGetAuthorizedWebSendTransferProjectsResult();
    ArrayOfProjectContainer arr1 = arr.getValue();
    List<ProjectContainer> arr2 = arr1.getProjectContainer();
    List<Project> projects = getPopulatedProjectList(arr2);
    return projects;
}

应用程序上下文.xml

application-context.xml

<int:chain input-channel="requestChannel" output-channel="outputChannel">
    <int-ws:header-enricher>
        <int-ws:soap-action
            value="http://tempuri.org/IPermissionService/GetAuthorizedWebSendTransferProjects"/>
    </int-ws:header-enricher>
    <int-ws:outbound-gateway
        uri="http://10.255.2.51/PermissionService.svc?wsdl" marshaller="marshaller"
        unmarshaller="marshaller" interceptor="addHttpHeaderInterceptor"/>
</int:chain>

我还有一个拦截器,可以将参数添加到静态数据的 http 标头中.

I also have an interceptor to add parameters to http headers which is an static data.

 @Override
public boolean handleRequest(MessageContext messageContext)
        throws WebServiceClientException {
    TransportContext context = TransportContextHolder.getTransportContext();
    HttpUrlConnection connection = (HttpUrlConnection) context.getConnection();
    HttpURLConnection connection1= connection.getConnection();
    connection1.addRequestProperty("user-email","ws_user1@biopacstest.domain");
    connection1.addRequestProperty("user-name","ws_user1");
    return true;
}

但我需要动态传递用户电子邮件"和用户名",而不是静态传递,即我在过滤器中收到的数据.任何人都可以帮我解决这个问题.提前致谢.

But I need to pass the "user-email" and "user-name" dynamically instead of the static one, i.e, the data which I have received in the filter . Can anybody help me out in solving this issue. Thanks in Advance.

推荐答案

解决方案是在拦截器中使用 ThreadLocal,然后在调用网关之前设置标头.

On solution would be to use a ThreadLocal in your interceptor, then set the headers before invoking the gateway.

private final ThreadLocal<MyHolder> holder = new ThreadLocal<MyHolder>();

@Override 
public boolean handleRequest(MessageContext messageContext)
        throws WebServiceClientException {
    TransportContext context = TransportContextHolder.getTransportContext();
    HttpUrlConnection connection = (HttpUrlConnection) context.getConnection();
    HttpURLConnection connection1= connection.getConnection();
    MyHolder holder = this.holder.remove();
    connection1.addRequestProperty("user-email", holder.getEmail());
    connection1.addRequestProperty("user-name", holder.getUser());
    return true;
}


public Message<?> setUserInfo(Message<?> message, String user, String email) {
    this.holder.set(new MyHolder(user, email));
    return message;
}

然后,在您的集成流程中,添加到chain...

Then, in your integration flow, add to the chain...

<int:service-activator expression=@interceptorBean.setUserInfo(#root, headers['user'], headers['email'])" />

(其中 MyHolder 是一个简单的 java bean).表达式可以是您想要的任何内容(不仅仅是标题访问).或者,只需传入 Message 并在代码内部确定用户/电子邮件.

(where MyHolder is a simple java bean). The expressions can be anything you want (not just header access). Or, just pass in the Message<?> and determine the user/email internally in the code.

这篇关于如何使用 spring 集成动态地通过 http 标头传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆