Saturday, June 24, 2017

Getting Http Header Param in Jersey (JAX-RS) Rest


How to read header parameter in Jersey REST - directly into a variable.



//contentType is the String variable which can be used later inside the method.
@GET
public Response getFirstUser(@HeaderParam("Content-Type") String contentType){

 ..
 ...
 System.out.println("content type:" + contentType)
  
 ..
  
}


There is one more way, by reading using @Context  HttpHeader (you can also use this in case you need to get multiple/all header parameters):

//All headers can be read by iterating over headers object.
@GET
public Response getFirstUser(@Context HttpHeaders headers){

  String contentType = headers.getRequestHeader("Content-Type");
  String userAgent= headers.getRequestHeader("user-agent").get(0);
  
  ...
  ... 
  
}





No comments:

Post a Comment

Prototype

Prototype is another creation pattern. Intent:  - Intent is to create objects by cloning existing instance  - Specify the kinds of obj...