我正在处理一个rest应用程序,我的rest应用程序总是通过http头部传递一些参数来调用。我的rest应用程序中有一个过滤器,它可以为每个请求调用并从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);
}
我的休息应用程序依次使用spring integration调用soap服务。调用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;
}
但是我需要动态地传递“user-email”和“user-name”而不是静态的,即我在过滤器中收到的数据。任何人都可以帮助我解决这个问题。在此先感谢。
答案 0 :(得分: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<?>
并在代码中内部确定用户/电子邮件。