Unable to make Resteasy work with Tomcat 8 and Java 8

1.6k views Asked by At

I am trying to make a simple resteasy based API using JAVA 8 (v45) and tomcat 8(v23), however I don't see my calls resulting in any traffic or log activity.

My web.xml looks like this

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd"
    version="3.0">

  <display-name>Test</display-name>

    <description>Test</description>

    <servlet>
        <servlet-name>resteasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy</servlet-name>
        <url-pattern>/restfulservices/*</url-pattern>
    </servlet-mapping>

    <!-- Auto scan REST service -->
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param>

    <!-- this should be the same URL pattern as the servlet-mapping property -->

    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/restfulservices</param-value>
    </context-param>

    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>

    <resource-ref>
        <description>Oracle Datasource example</description>
        <res-ref-name>jdbc/cboxDs</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    <!-- Environment entry examples -->
    <env-entry>
      <env-entry-name>test</env-entry-name>
      <env-entry-type>java.lang.String</env-entry-type>
      <env-entry-value>dev</env-entry-value>
    </env-entry>
  </web-app> 

and my service class looks like this:

package test.request.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.apache.log4j.Logger;

@Path("/dihService")
public class testService {

    private static final Logger LOGGER = Logger.getLogger(testService.class);

    @GET
    @Path("/hello")
    public Response getResponseMessage() {

        LOGGER.info(" Entered and param is :");
        return Response.status(200).entity(" Entered. Hello world").build();
    }
}

After I deploy, when i hit the following URL : http://localhost:10120/resteasy/restfulservices/dihService/hello

I expect some activity in the logs, however I am not seeing anything. I have already verified that my server is running on port 10120 and that my logs are otherwise flowing normally. Any ideas??

2

There are 2 answers

1
Juned Ahsan On

Your servlet config is mapped to URL that starts with /restfulservices as mentioned here:

  <servlet-mapping>
        <servlet-name>resteasy</servlet-name>
        <url-pattern>/restfulservices/*</url-pattern>
    </servlet-mapping>

but your actual web service class does not use that URL part

@Path("/dihService")
public class testService {

so it will not be found. You need add restfulservices to your class mapping path as mentioned here:

@Path("/restfulservices /dihService")
public class testService {
2
steffakasid On

I had a similar problem. I found out that in tomcat you always have an additional path (currently I don't know how to change that). Check the tomcat manager app http://localhost:8080/manager/html/list you'll find the path you need there for your deployed application. You should then be able to use

http://localhost:8080/somepath/restfulservices/dihService/

By the way I use the same structure of annotations like you did so the answer from Juned is not correct.

Regards Steffen