Saturday, 22 February 2014

How to read the Request Header parameters in Restful Service


About:

In this blog, I will be explaining on how to create Restful Service where we can read the Http Header Parameters in the Service Class

Code Snippet:
package project1;

import java.util.ArrayList;
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;

@Path("project1")
public class TestApp {
    @Context
    private HttpHeaders header;
    public TestApp() {
        super();
       
    }
    
    private ArrayList al = new ArrayList();
    @GET
    @Produces("application/xml")
    public String greet()
    {
        String userAgent =  header.getRequestHeader("UserType").get(0);  
        
        return ""+userAgent+"";
    }
}


In JAX-WS we can use the MessageContext to read the header parameters but in the caseof  Restful services we have to use the @Context parameter to get the Header values

Consuming In Client :

In my other blog i will be explaining on how to pass header parameters to service like above using WebService DataControl in Jdeveloper Link.