如何通过HTTP头动态使用Spring集成值传递(how to pass values throug

2019-10-21 19:45发布

我工作的一个支架应用,我的支架应用总是通过HTTP headers.And传递一些参数叫我在休息的应用程序的过滤器,其被调用为每个请求和如下所示检索来自HTTP标头的参数。

 @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);
}

我又将剩下的应用程序调用使用弹簧的SOAP服务integration.And代码调用SOAP服务是如下。

@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;
}

应用程序的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标头是一个静态数据。

 @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;
}

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

Answer 1:

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

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 ...

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

(其中MyHolder是一个简单的Java bean)。 该表达式可以是你想要的(不只是头接入)任何东西。 或者,只是通过在Message<?>并在内部确定所述代码的用户/电子邮件。



文章来源: how to pass values through http headers dynamically using spring integration