How to pass header parameters as method inputs in JAX-RS restful web services?
In this page you can see an example to pass http header info as a method input using @HeaderParam annotation.
In the previous examples we have given details of application setup, dependencies, web.xml file configurations: If you want to
know about these configuration, please refer these:
Restful web services using RESTEasy hello world example.
Restful web services using Jersey hello world example.
package com.java2novice.restful;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/http-header")
public class HttpHeaderService {
@GET
@Path("query")
public Response queryHeaderInfo(@HeaderParam("Cache-Control") String ccControl,
@HeaderParam("User-Agent") String uaStr){
String resp = "Received http headers are Cache-Control: "+ccControl+
"<br>User-Agent: "+uaStr;
return Response.status(200).entity(resp).build();
}
}
|
In the above example, if you use "http://localhost:8080/RestfulWebServices/http-header/query" URI pattern,
here is the output:
|