0
votes

Coding a JAXRS service using jersey and deployed on tomcat. Application subclass

@ApplicationPath("/rest")
public class RestApplication extends Application {

    public RestApplication() {
        // TODO Auto-generated constructor stub
        System.out.println("Inside RestApplication Constructor");
    }

    @Override
    public Set<Class<?>> getClasses() {
        // TODO Auto-generated method stub

        System.out.println("Get Class");
        Set<Class<?>> s=new HashSet<Class<?>>();
        s.add(SupportDataService.class);

        return s;
    }
}

Resource class

@Path("/supportdata")
public class SupportDataService {


    public SupportDataService() {
        // TODO Auto-generated constructor stub
        System.out.println("Inside SupportDataService Constructor");
    }

    @Path("/support")
    @GET
    @Produces(MediaType.TEXT_XML)
    public String getSupportData(){

        String xmlSupport=null;

        xmlSupport="<SupportData><Support><key>path1</key><value>value1</value></Support><Support><key>path2</key><value>value2</value></Support></SupportData>";

        return xmlSupport;
    }
}

Added all jersey jar in WEB-INF/lib except javax.servlet-api-3.0.1.jar and hitting url

http://localhost:8080/RestConfigurator/rest/supportdata/support

but getting 404 error. Not specified any web.xml as subclassed Application.

1
Did it print those statements out? - Paul Samsotha
No print statements were not executed. - NAZAR REHMAN
You might have to tell us step by step exactly how to reproduce the problem. Start a new simple project and record every step you take. Including the environment and how you deploy. List all the jars. Your code looks fine. URL seems to match up, given the war is RestConfigurator - Paul Samsotha
Added all the jars in jersey bundle .Earlier was not including jar javax.servlet-3.0.1.jar packaged in ext folder of jersey bundle. If include this jar the INFO: validateJarFile(D:\BATCH\UpdatedWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\RestConfigurator\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class. - NAZAR REHMAN
Added all jars of jersey bundle except javax.servlet-3.0.1.Deployed on tomcat6 with classes RestApplication and SupportDataService as posted above.No deployment web.xml included.Getting a 404error when accessing url localhost:8080/RestConfigurator/rest/supportdata/support. Apart from jersey bundle jar in WEB-INF\lib does jersey on tomcat requires any other configuration also. - NAZAR REHMAN

1 Answers

0
votes

So with Tomcat 6, which is a Servlet 2.5 implementation, we are required to have a web.xml declaring the ServletContainer. You can see more about it here. Basically, if you want to keep your RestApplication class, the web.xml would look something like

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>thepackge.of.RestApplication</param-value>
        </init-param>
    </servlet>

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

Given you have added all the jars from Jersey JAX-RS 2.0 RI bundle (besides the servlet-api), this should work. I've tested with Maven on Tomcat 6 and it works fine. Maven just pulls in all most of the jars from that bundle. But the app deployment doesn't differ.