Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restful webservice with jersey 2.0 without maven

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

like image 972
user2629427 Avatar asked Jul 29 '13 11:07

user2629427


People also ask

What is Jersey in RESTful web services?

Jersey is Sun's production quality reference implementation for JSR 311: JAX-RS: The Java API for RESTful Web Services. Jersey implements support for the annotations defined in JSR-311, making it easy for developers to build RESTful web services with Java and the Java JVM.

What is difference between Jersey and spring rest?

Jersey is the JAX-RS API example implementation provided by Sun, while Spring REST is of course Spring's implementation of the same API/JSRs. The major difference is that Spring REST easily integrates into other Spring APIs (if you wish) such as Spring Data Rest.


3 Answers

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

like image 194
laksys Avatar answered Oct 20 '22 03:10

laksys


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.

like image 27
grange74 Avatar answered Oct 20 '22 03:10

grange74


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

like image 30
user2629427 Avatar answered Oct 20 '22 03:10

user2629427