1 September 2015

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.

  1. asm_1.0.0.0_3-1.jar
  2. com.sun.jersey.client_1.0.0.0_1-9.jar
  3. com.sun.jersey.core_1.0.0.0_1-9.jar
  4. com.sun.jersey.json_1.0.0.0_1-9.jar
  5. com.sun.jersey.multipart_1.0.0.0_1-9.jar
  6. com.sun.jersey.server_1.0.0.0_1-9.jar
  7. jettison_1.0.0.0_1-1.jar
  8. org.codehaus.jackson.core.asl_1.0.0.0_1-8-3.jar
  9. org.codehaus.jackson.jaxrs_1.0.0.0_1-8-3.jar
  10. org.codehaus.jackson.mapper.asl_1.0.0.0_1-8-3.jar
  11. org.codehaus.jackson.xc_1.0.0.0_1-8-3.jar
  12. rome_1.0.0.0_1-0.jar 
These above jar comes along with weblogic 10.x package as well. or you can download these jars separately also.

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;
     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;

public class TestWS {

    public String sayPlainTextHello() {
        return "Hello World from RESTful Jersey!";

    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> JerSey Webservice in XML Format"
                + "</hello>";

    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello World RESTful Jersey"
                + "</title>" + "<body><h1>" + "Hello World RESTful Jersey"
                + "</body></h1>" + "</html> ";
    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";
    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-name>Jersey REST Service</servlet-name>
    <servlet-name>Jersey REST Service</servlet-name>

Now run the application by deploying it on the weblogic server and hit the following URL in the browser:

you will see the following output.

when you open the URL for XML you will get the following output.

If you open the URL for simple String output you will get the following result:

If you open the URL for JSON output you will get the following result:

Happy Coding :)

0 on: "Hello World restful webservice with jersey filter."