Hello World restful webservice with jersey filter.
Hi All,
In this post I am going to show you how you can create the Hello World restful webservice with jersey filter or jersey interceptor.
In this example I am also going to show you how you can deal with different types of response like simple String, HTML, XML or JSON.
For getting started with this prerequisites are as follows:
1) You should have a eclipse IDE on you system (in my case Luna is the version of eclipse)
2) You should have JDK installed on you system (in my case jdk 1.7)
3) You should have any server installed on your system (in my case weblogic 10.3.6)
Now let's start by creating a fresh project in eclipse. To create the fresh project click on "File" then "New" and then select "Project" from available option as shown below:
After this you will get the following screen where you need to select "Dynamic Web Project" from the available option.
Now click on Next. on next screen provide the Project name and click on "Next".
Click on "Next".
Now click on "Next".
Once you click on Finish, the below screen will appear. Click on "Yes".
After this you can see that a project is created in the eclipse project explorer as shown below:
Now put the following Jars inside the lib folder of WEB-INF.
If you want to get these jars from weblogic these will be located at location "<WL_SERVER_HOME>/common/deployable-libraries" with name "jersey-bundle-1.9.war". Once you unzip it you can see these jars./
Now right click on "src" folder and select "Class" inside the "New" option as shown below:
Give the name of the package as "test" and class name as "PersonDetails" and click on Finish.
And write the following code inside this method:
package test;
public class PersonDetails {
private int id;
private String firstName;
private String lastName;
private String email;
public PersonDetails(String fname, String lname, int id, String email){
this.firstName = fname;
this.lastName = lname;
this.id = id;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return new StringBuffer(" First Name : ").append(this.firstName).append(" Last Name : ").append(" ID : ").append(this.id).append(this.lastName).append(" Email : ").append(this.email).toString();
}
}
Now create one more class and give the name of the package as "test" and class name as "TestWS" and click on Finish.
Now write the following code inside this class.
package test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/helloworld")
public class TestWS {
@GET
@Path("/sayStringHello")
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello World from RESTful Jersey!";
}
@GET
@Path("/sayXMLHello")
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> JerSey Webservice in XML Format"
+ "</hello>";
}
@GET
@Path("/sayHTMLHello")
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello World RESTful Jersey"
+ "</title>" + "<body><h1>" + "Hello World RESTful Jersey"
+ "</body></h1>" + "</html> ";
}
@GET
@Path("/sayJsonHello")
@Produces(MediaType.APPLICATION_JSON)
public PersonDetails sayJsonHello() {
PersonDetails pd=new PersonDetails("Kunal", "Kumar", 1, "kumar.kunal2808@gmail.com");
return pd;
}
}
Now create another class inside test package as "RestAuthenticationFilter" and write the following code inside it.
package test;
import java.io.IOException;
import javax.ws.rs.WebApplicationException;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
public class RestAuthenticationFilter implements ContainerRequestFilter {
public static final String AUTHENTICATION_HEADER = "Authorization";
@Override
public ContainerRequest filter(ContainerRequest request) throws WebApplicationException {
System.out.println("Inside the filter");
String authCredentials = request.getHeaderValue(AUTHENTICATION_HEADER);
boolean authenticationStatus= true;
if (!authenticationStatus) {
System.out.println("Inside the doFilter Method authentication is not true");
}
System.out.println("Inside the *********** doFilter Method authentication is true");
return request;
}
}
Now update the web.xml of the project as follows:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Hello World</display-name>
<servlet>
<servlet-name>Jersey REST Service</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>test</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>test.RestAuthenticationFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
</web-app>
Now run the application by deploying it on the weblogic server and hit the following URL in the browser:
http://localhost:7001/HelloWorldRest/rest/helloworld/sayHTMLHello
you will see the following output.
when you open the URL for XML you will get the following output.
http://localhost:7001/HelloWorldRest/rest/helloworld/sayXMLHello
If you open the URL for simple String output you will get the following result:
http://localhost:7001/HelloWorldRest/rest/helloworld/sayStringHello
If you open the URL for JSON output you will get the following result:
http://localhost:7001/HelloWorldRest/rest/helloworld/sayJsonHello
Happy Coding :)
In this post I am going to show you how you can create the Hello World restful webservice with jersey filter or jersey interceptor.
In this example I am also going to show you how you can deal with different types of response like simple String, HTML, XML or JSON.
For getting started with this prerequisites are as follows:
1) You should have a eclipse IDE on you system (in my case Luna is the version of eclipse)
2) You should have JDK installed on you system (in my case jdk 1.7)
3) You should have any server installed on your system (in my case weblogic 10.3.6)
Now let's start by creating a fresh project in eclipse. To create the fresh project click on "File" then "New" and then select "Project" from available option as shown below:
After this you will get the following screen where you need to select "Dynamic Web Project" from the available option.
Now click on Next. on next screen provide the Project name and click on "Next".
Click on "Next".
Now click on "Next".
Once you click on Finish, the below screen will appear. Click on "Yes".
After this you can see that a project is created in the eclipse project explorer as shown below:
Now put the following Jars inside the lib folder of WEB-INF.
- asm_1.0.0.0_3-1.jar
- com.sun.jersey.client_1.0.0.0_1-9.jar
- com.sun.jersey.core_1.0.0.0_1-9.jar
- com.sun.jersey.json_1.0.0.0_1-9.jar
- com.sun.jersey.multipart_1.0.0.0_1-9.jar
- com.sun.jersey.server_1.0.0.0_1-9.jar
- jettison_1.0.0.0_1-1.jar
- org.codehaus.jackson.core.asl_1.0.0.0_1-8-3.jar
- org.codehaus.jackson.jaxrs_1.0.0.0_1-8-3.jar
- org.codehaus.jackson.mapper.asl_1.0.0.0_1-8-3.jar
- org.codehaus.jackson.xc_1.0.0.0_1-8-3.jar
- rome_1.0.0.0_1-0.jar
If you want to get these jars from weblogic these will be located at location "<WL_SERVER_HOME>/common/deployable-libraries" with name "jersey-bundle-1.9.war". Once you unzip it you can see these jars./
Now right click on "src" folder and select "Class" inside the "New" option as shown below:
And write the following code inside this method:
package test;
public class PersonDetails {
private int id;
private String firstName;
private String lastName;
private String email;
public PersonDetails(String fname, String lname, int id, String email){
this.firstName = fname;
this.lastName = lname;
this.id = id;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return new StringBuffer(" First Name : ").append(this.firstName).append(" Last Name : ").append(" ID : ").append(this.id).append(this.lastName).append(" Email : ").append(this.email).toString();
}
}
Now create one more class and give the name of the package as "test" and class name as "TestWS" and click on Finish.
package test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/helloworld")
public class TestWS {
@GET
@Path("/sayStringHello")
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello World from RESTful Jersey!";
}
@GET
@Path("/sayXMLHello")
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> JerSey Webservice in XML Format"
+ "</hello>";
}
@GET
@Path("/sayHTMLHello")
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello World RESTful Jersey"
+ "</title>" + "<body><h1>" + "Hello World RESTful Jersey"
+ "</body></h1>" + "</html> ";
}
@GET
@Path("/sayJsonHello")
@Produces(MediaType.APPLICATION_JSON)
public PersonDetails sayJsonHello() {
PersonDetails pd=new PersonDetails("Kunal", "Kumar", 1, "kumar.kunal2808@gmail.com");
return pd;
}
}
Now create another class inside test package as "RestAuthenticationFilter" and write the following code inside it.
package test;
import java.io.IOException;
import javax.ws.rs.WebApplicationException;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
public class RestAuthenticationFilter implements ContainerRequestFilter {
public static final String AUTHENTICATION_HEADER = "Authorization";
@Override
public ContainerRequest filter(ContainerRequest request) throws WebApplicationException {
System.out.println("Inside the filter");
String authCredentials = request.getHeaderValue(AUTHENTICATION_HEADER);
boolean authenticationStatus= true;
if (!authenticationStatus) {
System.out.println("Inside the doFilter Method authentication is not true");
}
System.out.println("Inside the *********** doFilter Method authentication is true");
return request;
}
}
Now update the web.xml of the project as follows:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Hello World</display-name>
<servlet>
<servlet-name>Jersey REST Service</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>test</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>test.RestAuthenticationFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
</web-app>
Now run the application by deploying it on the weblogic server and hit the following URL in the browser:
http://localhost:7001/HelloWorldRest/rest/helloworld/sayHTMLHello
you will see the following output.
when you open the URL for XML you will get the following output.
http://localhost:7001/HelloWorldRest/rest/helloworld/sayXMLHello
If you open the URL for simple String output you will get the following result:
http://localhost:7001/HelloWorldRest/rest/helloworld/sayStringHello
If you open the URL for JSON output you will get the following result:
http://localhost:7001/HelloWorldRest/rest/helloworld/sayJsonHello
Happy Coding :)
Comments
Post a Comment