JAX-RS @HeaderParam. Reading Request Headers.

We can use @HeaderParam annotation to read the Request HTTP Headers when building RESTFul Web Services. And knowing how to use @PathParam and @QueryParam annotations, the use of @HeaderParam annotation becomes very obvious. We just use it the same way as we use the @QueryParam for example.

JAX-RS @HeaderParam Annotation Code Example.

JAX-RS gives us a couple of ways to read header values of the request. One way is to annotate with @HeaderParam the method argument like in the code example below. Method arguments contentType and loginToken will be assigned values of Content-Type and login-token Request Headers.

@GET 
    @Path("/{userId}/videos/{videoId}/process")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response startVideoProcessing(
            @HeaderParam("Content-type") String contentType,
            @HeaderParam("login-token") String loginToken,
            @PathParam("userId") String userId,
            @PathParam("videoId") String videoId)
    {
        // Process video with id 
        System.out.println("Starting to process video with id " + videoId);
        
        // ... 
        StringBuilder responseString = new StringBuilder();
        responseString.append("Video processing was requested for video with id: ");
        responseString.append(videoId);
        responseString.append(" and login-token value:  ");
        responseString.append(loginToken);
        responseString.append(" and request Content-type value:  ");
        responseString.append(contentType);
        
        return Response.status(200)
      .entity(responseString.toString())
      .build();
    }

Reading HttpHeaders with @Context Annotation

Another way to read HTTP Header values from a Request is to use HttpHeaders object and @Context annotation. This way is especially convenient when there are many header values you need to read and would like to keep your method signature short and nice.

In the code example below all HTTP Headers of the Request will be available via a single HttpHeaders object.

@GET 
@Path("/{userId}/videos/{videoId}/process")
@Consumes(MediaType.APPLICATION_JSON)
public Response startVideoProcessing(
        @Context HttpHeaders requestHeaders,
        @PathParam("userId") String userId,
        @PathParam("videoId") String videoId)
{
    // Process video with id 
    System.out.println("Starting to process video with id " + videoId);
    
    String contentType = requestHeaders.getRequestHeader("Content-type").get(0);
    String loginToken = requestHeaders.getRequestHeader("login-token").get(0);
    
    // ... 
    StringBuilder responseString = new StringBuilder();
    responseString.append("Video processing was requested for video with id: ");
    responseString.append(videoId);
    responseString.append(" and login-token value:  ");
    responseString.append(loginToken);
    responseString.append(" and request Content-type value:  ");
    responseString.append(contentType);
    
    return Response.status(200)
  .entity(responseString.toString())
  .build();
}

Building RESTful Web Services is so much fun especially when you know the subject very well. Check out the video courses below which will definitely help you become a very good specialist and a RESTFul Web Services developer.

Java Web Services. Video Course.

Learn how to design,create , consume and secure SOAP and REST web services from scratch in easy steps.

Java
icon

Java Web Services Part 2. Video Course.

Master advanced web services concepts and implement them in easy steps

Java Web Services Part 2
icon

REST Java Web Services. Video Course.

A guide to understanding, accessing, and writing a REST Java web service using Apache and Java EE.

Java Web Services Part 2
icon