如何访问 Spring-ws 端点中的 HTTP header ?
我的代码是这样的:
public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
protected Object invokeInternal(Object arg) throws Exception {
MyReq request = (MyReq) arg;
// need to access some HTTP headers here
return createMyResp();
}
}
invokeInternal()
仅获取未编码的 JAXB 对象作为参数。如何访问 invokeInternal()
中的请求随附的 HTTP header ?
一种可能可行的方法是创建一个 Servlet 过滤器,将 header 值存储到 ThreadLocal
变量,然后在 invokeInternal()
中访问该变量,但是有没有更好的方法,更像 Spring 的方式来做到这一点?
最佳答案
您可以添加这些方法。 TransportContextHolder
将在线程局部变量中保存一些与传输(在本例中为 HTTP)相关的数据。您可以从 TransportContext
访问 HttpServletRequest
.
protected HttpServletRequest getHttpServletRequest() {
TransportContext ctx = TransportContextHolder.getTransportContext();
return ( null != ctx ) ? ((HttpServletConnection ) ctx.getConnection()).getHttpServletRequest() : null;
}
protected String getHttpHeaderValue( final String headerName ) {
HttpServletRequest httpServletRequest = getHttpServletRequest();
return ( null != httpServletRequest ) ? httpServletRequest.getHeader( headerName ) : null;
}
关于java - 如何访问 Spring-ws 端点中的 HTTP header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3975694/