Get HTTP Header Parameter in JAX-RS

When you need to get HTTP Header parameters with JAX-RS, you have a couple of options. You can use the @HeaderParam annotation provided by JAX-RS, Inject the HttpHeaders directly or inject the HttpServletRequest. Let’s show you by some concrete examples.

Get HTTP Header with @HeaderParam

You can use the @HeaderParam annotation to inject HTTP request header values directly in your method parameter. You must provide the HTTP Header case-sensitive key in the annotation and the binding is done automaticaly.

package com.memorynotfound.rs;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/orders")
public class OrderResource {

    @GET
    public Response getOrders(@HeaderParam("user-agent") String userAgent){

        return Response
                .ok()
                .entity("getOrders() user-agent: " + userAgent)
                .build();
    }

}

The previous code generates the following output:

getOrders() user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

Get HTTP Header with HttpServletRequest

The second option is to inject the HttpServletRequest using the @Context annotation. When you obtain the HttpServletRequest you can retrieve the HTTP request headers from it.

package com.memorynotfound.rs;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

@Path("/products")
public class ProductResource {

    @GET
    public Response getProducts(@Context HttpServletRequest request){

        String userAgent = request.getHeader("user-agent");

        return Response
                .ok()
                .entity("getProducts() user-agent: " + userAgent)
                .build();
    }

}

The previous code generates the following output:

getProducts() user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

Get HTTP Header by Injecting HttpHeader

The last option is to inject the HttpHeaders directly in the method parameters using the @Context annotation. This class is provided by JAX-RS and can retrieve header information.

package com.memorynotfound.rs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserResource {

    @GET
    public Response getUser(@Context HttpHeaders headers){

        String userAgent = headers.getRequestHeader("user-agent").get(0);

        return Response
                .ok()
                .entity("getUser() user-agent: " + userAgent)
                .build();
    }

}

The previous code generates the following output:

getUser() user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

References

Download

Download it – get-http-headers-jaxrs

You may also like...