J Fabian Meier
J Fabian Meier

Reputation: 35785

Jersey application deployed on Tomcat returns 404

I tried to write a REST service and start it with Tomcat. My web.xml is:

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

    <display-name>TomcatRestExample</display-name>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
              <param-name>jersey.config.server.provider.packages</param-name>
              <param-value>de.continentale.testsvn.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/maven/*</url-pattern>
    </servlet-mapping>

</web-app>

My class de.continentale.testsvn.rest.RestCaller looks like:

package de.continentale.testsvn.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/service")
public class RestCaller {

    @GET
    @Produces(MediaType.TEXT_XML)
    public String convert() {
        return "<text>hallo</text>";
    }
}

I tried to access http://localhost:8090/TomcatRestExample/maven/service but got:

HTTP Status 404 - /TomcatRestExample/maven/service

type Status report

message /TomcatRestExample/maven/service

description The requested resource is not available.

Apache Tomcat/7.0.41

Tomcat console output in Eclipse:

INFORMATION: Reloading Context with name [/tomcat-rest-test] has started
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.servlet.WebAppResourceConfig init
INFORMATION: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFORMATION: Root resource classes found:
  class de.continentale.testsvn.rest.RestCaller
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFORMATION: No provider classes found.
Apr 20, 2017 2:23:19 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 05:39 AM'
Apr 20, 2017 2:23:20 PM org.apache.catalina.core.StandardContext reload
INFORMATION: Reloading Context with name [/tomcat-rest-test] is completed

Any idea what I am missing?

Upvotes: 0

Views: 559

Answers (2)

cassiomolin
cassiomolin

Reputation: 130837

Looks like your context is not correct. Your application is deployed at /tomcat-rest-test and not at /TomcatRestExample. The latter is just the display name used by admin tools. Tomcat uses the WAR file name (without .war) as the context name.

So use http://localhost:8090/tomcat-rest-test/maven/service to hit your resource method.

Upvotes: 3

aUserHimself
aUserHimself

Reputation: 1597

It seems your context is /tomcat-rest-test not /TomcatRestExample. Try the first one: localhost:8090/tomcat-rest-test/maven/service.

Upvotes: 1

Related Questions