Question

Can anybody tell me how to make a restful web service with Jersey 2.0 by not using maven. I have searched everywhere and found tutorial for Jersey1.x versions but not for 2.0. Please help

Was it helpful?

Solution 2

I found the answer

package com.hellowebservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

@GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

}

Web.xml

   <?xml version="1.0" encoding="UTF-8"?>
   <web-app id="WebApp_ID" version="2.5"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <display-name>FirstRestWebService</display-name>
    <servlet>
   <display-name>Rest Servlet</display-name>
  <servlet-name>RestServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <init-param>
       <param-name>javax.ws.rs.Application</param-name>
           <param-value>com.hellowebservice.MyApplication</param-value>
            </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
     <servlet-mapping>
   <servlet-name>RestServlet</servlet-name>
  <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>
 </web-app>

MyApplication.java

package com.hellowebservice;
   import org.glassfish.jersey.server.ResourceConfig;


   public class MyApplication extends ResourceConfig {
      public MyApplication() {
          packages("com.hellowebservice");
    }
   }

run with localhost:8080/FirstRestWebService/rest/hello

OTHER TIPS

We provide detail answere based on the user answer user2629427. we checked this on windows 7.

Requirement: (brackets indicate version which this example is tested)

  • tomcat (8 zip version)
  • jersey (2.x)

Unzip the tomcat & create a below folder structure in tomcat's 'webapps' folder (folder names are case sensitive).

abc
  |___ WEB-INF
      |____ classes
      |____ lib

Put 'Hello.java' and 'MyApplication.java' into 'classes' folder and 'web.xml' into 'WEB-INF' folder.

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" 
    version="3.1">  

    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.king.MyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

Myapplication.java

package com.king;

import org.glassfish.jersey.server.ResourceConfig;

public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("com.king");
    }
}

Hello.java

package com.king;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey";
    }

    // This method is called if XML is request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?><hello>Hello Jersey</hello>";
    }

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html><title>Hi Jersey</title><body><h1>Hello Jersey this is laksys</body></h1></html>";
    }
}

Unzip jersey and copy all jar files from api, ext, and lib (not folders) into your apps 'lib' folder.

Now compile the two java files using following command

D:\apache-tc-8\webapps\abc\WEB-INF\classes>javac -d . -cp ..\lib\javax.ws.rs-api-2.0.1.jar;..\lib\jersey-server.jar;..\l ib\jersey-common.jar *.java

Next run the tomcat server

D:\apache-tc-8\bin>startup

In browser address bar type this: http://localhost:8080/abc/rest/hello

Just to add to the previous answer. If you aren't using Maven and just building using Eclipse with a Dynamic Web Project and deploying to web app server like Tomcat.

Just download the Jersey JAX-RS 2.0 RI bundle Jersey Downloads, unzip and add all the jars in the lib, api and ext folders to your build path. (I tried without ext jars but got classnotfound when starting the server).

Also add all the jars to the Deployment Assembly of your Dynamic Web Project so they get automatically copied to the WEB-INF/lib directory when deployed to your web app server. Along with the code & web.xml in the above answer, you should have a RESTful api using Jersey 2 up and running.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top