Can I get request header and response header for web service function?

1.3k views Asked by At

I am using Java with Apache CXF to write the backend for AngularJS (single-page web site). In my REST service, I need access to the header of the http request (i.e. parameters and cookies) and I need access to the response header also (i.e. parameters and cookies). The main reason for this is security, authentication purposes and session management. Those are important reasons.

Is there a way of getting both of these structures in Apach CXF RESTfull code?

1

There are 1 answers

1
Karthik Prasad On

You can inject request by using @Context [javax.ws.rs.core.Context]

  public Response myRest(@Context HttpServletRequest request /*, other parameters if you have like @QueryParam */ ){
    request.getCookies();
    request.getUserPrincipal();
}

You can set cookie or header in response as bellow

ResponseBuilder builder = Response.ok(); //Response.status(500) either way
builder.cookie(arg0);
builder.header(arg0, arg1);
return bulider.build();