Searching...
Saturday, 9 January 2016

JAX-RS HttpHeaders

00:23
JAX-RS - how to get http headers in the service
In this example we will see how to get the http headers whenever JAX-RS provider receives http request.
package com.vinod.vinod_rest_examples;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
@Path("/request")
public class MyhttpHeaders {
/**
*http://localhost:8080/request/httpHeaders
*@param headers
*@return
*/
@GET
@Path("httpHeaders")
@Produces(MediaType.APPLICATION_JSON)
public String test(@Context HttpHeaders headers) {
return headers.getHeaderString("user-agent");
}
}
Hit the url 
http://localhost:8080/request/httpHeaders
 
Output !!!
 
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36

0 comments: