Sunday, December 13, 2015

Apache Jersey + Tomcat + Eclipse + Maven with “echo” in XML/JSON (REST GET) in less than 30 min. with troubleshooting tips

Disclaimer:
Started with simple settings (with not even JUnit J.)

Pre-requisite:
1.       Jdk (tested with 1.8)
2.       Tomcat (tested with 8)
3.       Eclipse (tested with Mars)

Steps:
1.       Create a Maven project in Eclipse
Create a blank web-project with maven-archtype-web
2.       Add dependencies in Maven
a.       Add jersey-bundle
b.       Ensure java 1.8 is used (not 1.5 as the default is set)
Pom.xml:
      <dependencies>
            <dependency>
                  <groupId>com.sun.jersey</groupId>
                  <artifactId>jersey-bundle</artifactId>
                  <version>1.19</version>
            </dependency>
      </dependencies>
      <build>
            <finalName>pidev</finalName>
            <plugins>
                  <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.1</version>
                        <configuration>
                              <source>1.8</source>
                              <target>1.8</target>
                        </configuration>
                  </plugin>
            </plugins>
      </build>
3.       Add Tomcat server library in Eclipse AND include in Order and Export
·         Build Path -> Configure Build Path -> Libraries -> Add Library -> Server Runtime -> Apache Tomcat 8.0
4.       Configure Jersey to listen for URI
       <servlet>
            <servlet-name>Jersey Web Application</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><your package></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
            <servlet-name>Jersey Web Application</servlet-name>
            <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>


(Note that if the url-pattern is /* then url to address doesn’t need “rest” in the URI J)
5.       Write code
 package <your package>;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/xmlapi")
public class XMLAPI {
      @GET
      @Produces("application/xml")
      public String echo() {
            return ("<xmlapi>default echo</xmlapi>");
      }

      @Path("{c}")
      @GET
      @Produces("application/xml")
      public String echo(@PathParam("c") String c) {
            return ("<xmlapi>"+c+"</xmlapi>");
      }
}



For json
 package <your package>;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/jsonapi")
public class JsonAPI {
      @GET
      @Produces("application/json")
      public String echo() {
            return "{\"message\": \"default echo\"}";
      }

      @Path("{c}")
      @GET
      @Produces("application/xml")
      public String echo(@PathParam("c") String c) {
            return "{\"message\": \"" + c + "\"}";
      }
     

}
6.       Build and deploy
·         Maven -> Update project
·         Run project in Apache Tomcat
7.       Access the service
Output:
<?xml version="1.0"?>
<xmlapi>default echo</xmlapi>
Output:
<?xml version="1.0"?>

<xmlapi>hi</xmlapi>

Potential problems:
1.       Project don’t compile:
Java compiler level does not match the version of the installed Java project facet.
JAX-RS (REST Web Services) 2.0 requires Java 1.6 or newer.
One or more constraints have not been satisfied.
Maven default uses 1.5 J
Solution:
Add/update build element of pom.xml
      <build>
            <finalName><Project name></finalName>
            <plugins>
                  <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.1</version>
                        <configuration>
                              <source>1.8</source>
                              <target>1.8</target>
                        </configuration>
                  </plugin>
            </plugins>
      </build>

2.       Jersey didn’t load your mapping
com.sun.jersey.api.core.ScanningResourceConfig.init No provider classes found.
Solution:
In web.xml ensure that correct package is specified in init-param
      <servlet>
            <servlet-name>Jersey Web Application</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><your package></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
      </servlet>

3.       Status 500 UriBuilder
Duplicate UriBuilder J Usually it happens if you try different dependencies and one of those dependencies downloads it for you (e.g. jersey-bundle with jersey-server or jersey-apache-connector)
Solution:
Remove javax.ws.rs-api.jar. 

No comments:

Post a Comment