Step by Step example to build REST Service with JAX-RS

REST service with Java JAX-RS step by step:

  1. Include following to two mapping in your web.xml
  2.  
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/yourAppURL/*</url-pattern>
    </servlet-mapping>
    
  3. Create a class for REST Service. The @PATH annotation must be defined at the class level not at the method level.
  4. /**
    * @author shirish
    * @Jul 11, 2016
    *
    */
    @Path("/hello")
    public class HelloWorldClass {
    
    /**
    *
    */
    public HelloWorldClass() {
    
      }
    
    @GET
    @Produces("text/plain")
    public String getClichedMessage() {
    
        return "Hello World";
      }
    }
    
  5. Classpath in the war.  –
  6. The easiest way to get war working in a java web container is to put all application classes in “yourWebApp/WEB-INF/classes” such that you do not need to fix your application classpath to include jars.

    Note: even if you put your application jars in “yourWebApp/WEB-INF/lib”, the jars may not be in classpath properly; therefore, may not have the classes loaded that are necessary.

  7. Build and Deploy in JETTY or any application server.
  8. path to get to your REST service
  9.     http://localhost:8080/yourapp/yourAppURL/hello
    

Reference: