Tuesday 15 February 2011

java - JAXRS CXF - get http headers without specifying as method parameters -


Is it possible to get HTTP headers within the JAXRS processing method without specifying these headers as a method parameter ?

  @ path ("/ post") public interface postal resources {@GET public list & lt; Post & gt; GetAllPosts (); }  

and the following classes that implement this interface:

  public class postal resources IMPL implementation of postal resources {@Autowired Private PostsService PostsService; Public listing & lt; Post & gt; GetAllPosts () {Back PostsServices All posts (); }}  

I do not want to change my method signature to:

  public list & lt; Post & gt; GetAllPosts (@HeaderParam ("X-MyCustomHeader") string myCustomHeader);  

This header will be connected by the interceptor on the client side so that the client code does not know what should be kept here and this should not be a clear method parameter.

You can use either HttpHeaders Type of object can be injected, as described below:

  @ path ("/ test") Public Category TestService {@Context Private HTTP header header; @GET @ path ("/ {pathParameter}") Public Response Testing Method () {(...) List & lt; String & gt; CustomHeaderValues ​​= headers.getRequestHeader ("X-mikheimer"); System.out.println ("& gt; & gt; X-MyCustomHeader =" + CustomHeaderWell); (...) String response = (...) Response. Stats (200). Entity (feedback) .build (); }}  

Hope to answer your question Thierry


No comments:

Post a Comment