1

I am new to Java Web-Services and I am trying to implement a simple JAX-RS example using Jersey implementation with my Eclipse IDE and deploying the application on Apache Tomcat 6.0 as a plugin for my Eclipse. I am following the example given in book java web services up and running

I have created a JAX-RS Application:

package adages;

import java.util.Set;
import java.util.HashSet;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/resourcesA")
public class RestfulAdage extends Application {
    @Override
    public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<Class<?>>();
        set.add(Adages.class);
        return set;
    }
}

and JAX-RS resource:

package adages;

import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Random;
import com.fasterxml.jackson.databind.ObjectMapper;

@Path("/")
public class Adages {

    private String[] aphorisms = {"What can be shown cannot be said.",
        "If a lion could talk, we could not understand him."};

    public Adages() {
    }

    @GET
    @Produces({ MediaType.APPLICATION_XML })
    public JAXBElement<Adage> getXml() {
        return toXml(createAdage());
    }

    @GET
    @Produces({ MediaType.APPLICATION_JSON })
    @Path("/json")
    public String getJson() {
        return toJson(createAdage());
    }

    @GET
    @Produces({ MediaType.TEXT_PLAIN })
    @Path("/plain")
    public String getPlain() {
        return createAdage().toString() + "\n";
    }

    // Create an Adage and set the words property, which
    // likewise sets the wordCount property. The adage is
    // randomly selected from the array, aphorisms.
    private Adage createAdage() {
        Adage adage = new Adage();
        adage.setWords(aphorisms[new Random().nextInt(aphorisms.length)]);
        return adage;
    }

    // Java Adage --> XML document
    @XmlElementDecl(namespace = "http://aphorism.adage", name = "adage")
    private JAXBElement<Adage> toXml(Adage adage) {
        return new JAXBElement<Adage>(new QName("adage"), Adage.class, adage);
    }

    // Java Adage --> JSON document
    // Jersey provides automatic conversion to JSON using the Jackson
    // libraries. In this example, the conversion is done manually
    // with the Jackon libraries just to indicate how straightforward it is.
    private String toJson(Adage adage) {
        String json = "If you see this, there's a problem.";
        try {
            json = new ObjectMapper().writeValueAsString(adage);
        } catch (Exception e) {
        }
        return json;
    }
}

and my POJO class:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "adage")
public class Adage {
    protected String words;
    protected int wordCount;

    public Adage() {
    }

// overrides
@Override
public String toString() {
        return words + " -- " + wordCount + " words";
    }
           // Setters & Getters
}

the web.xml file has:

<servlet>
    <servlet-name>jersey</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>adages</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

When I deploy my application to Tomcat server then I see below messages in logs:

INFO: Starting Servlet Engine: Apache Tomcat/6.0.36
Jan 26, 2014 10:19:03 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  adages
Jan 26, 2014 10:19:03 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class adages.Adages
Jan 26, 2014 10:19:03 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Jan 26, 2014 10:19:03 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.10 11/02/2011 03:53 PM'

It says that "No provider classes found".

When I try to access the URL - http://localhost:8080/rest/resourcesA/, then I am getting below error:

HTTP Status 404 - /rest/resourcesA/

Here "rest" is the name of my eclipse project. Please help me where I am doing mistake in this example?

I have gone through this post : 404 when accessing Jersey app in Tomcat 7, but I still face the same issue.

This is very basic example and I am stuck with why it is not working, gone through several posts but I am not able to get a clue for this issue, please let me know where I am doing mistake?

Community
  • 1
  • 1
Chaitanya
  • 15,403
  • 35
  • 96
  • 137

1 Answers1

1

After browsing further I am able to solve the issue now.

1) RestfulAdage class is not required, so I removed it.

2) Updated my web.xml file to have a URL mapping for Jersey's ServletContainer

<servlet-mapping>
   <servlet-name>jersey</servlet-name>
   <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Now accessing the URL as http://localhost:8080/rest/rest invoked the method public JAXBElement<Adage> getXml()

and accessing the URL as http://localhost:8080/rest/rest/plain invoked the method public String getPlain() successfully.

Chaitanya
  • 15,403
  • 35
  • 96
  • 137