How can I access HTTP headers in Spring-ws endpoint?
My code looks like this:
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()
gets only the unmarshalled JAXB object as the parameter. How can I access HTTP headers that came with the request inside invokeInternal()
?
One way that would probably work is to create a Servlet filter that stores header values to ThreadLocal
variable that is then accessed inside invokeInternal()
, but is there a nicer, more spring-like way to do this?
You can add these methods. The
TransportContextHolder
will hold some data related to transport (HTTP in this case) in a thread local variable. You can accessHttpServletRequest
from theTransportContext
.I had the same kind of problem (see this other question). I needed to add a Content-Type header to my WS. I went the road of the Servlet Filter. Most of the time, you should not need to change HTTP headers in a webservice. But ... there is sometime a diference between theory and practice.
You can access to HTTP headers in Spring SOAP Endpoint by injecting HttpServletRequest.
For example, you need to get Autorization header (you use Basic authentication).
SOAP request:
@Endpoint java class