Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 30 November 2013

JAX-RS Get HTTP Headers using @HeaderParam


In this tutorial we are going to see how to get HTTP request header using JAX-RS  RESTful web service @HeaderParam
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below.
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
add jar
4. Create package called com.javatutorialscorner.jaxrs.httpheader under RESTful-WebService
5. Create Java class HTTPHeaderService1 under com.javatutorialscorner.jaxrs.httpheader package
HTTPHeaderService1.java
package com.javatutorialscorner.jaxrs.httpheader;

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

@Path("/tutorials")
public class HTTPHeaderService1 {

@GET
@Path("/java-tutorial")
public Response getTutorial(@HeaderParam("user-agent") String userAgent) {
return Response
.status(200)
.entity("User Agent Details : " + userAgent).build();
}
}

6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JAX-RS-Path</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.javatutorialscorner.jaxrs.httpheader</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL

Now you can run the service and access the service by calling the following URL

http://localhost:8080/RESTful-WebService/rest/tutorials/java-tutorial

output

output

Note : in this tutorials I used user-agent alone. You can access all http headers using @HeaderParam

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • RESTful Web service with RESTEasy Client In this tutorials we are going to see the how to access the RESTful web service using  RESTEasy client. Here i am going to create two clients to access GET and POST request GET Request Refer this example for creatin… Read More
  • RESTful Web service with Apache HttpClient In this tutorials we are going to see the how to access the RESTful web service using Apache HttpClient. Here i am going to create two clients to access GET and POST request Add httpcore-4.1.jar, httpclient-4.1.1.jar, comm… Read More
  • JAX-RS RESTful Web service to consume JSON and Produce XMLIn this tutorial we are going to see how to consume JSON and Produce XML using RESTful web service1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-W… Read More
  • JAX-RS Send List using QueryParam AnnotationIn this tutorial we are going to see how to send list to RESTful web service via URL using @QueryParam1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below. 2. Create the Projec… Read More
  • File Upload using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to upload file using Jersey RESTful web service 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-WebServic… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-RS Get HTTP Headers using @HeaderParam Rating: 5 Reviewed By: eHowToNow