Reputation:
I'm getting a 404 NOT FOUND exception when I try and run my webservice on tomcat 8. My pom looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.deangrobler.testrest</groupId>
<artifactId>TestRest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
</project>
And my web.xml looks like:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>TestRest</display-name>
<servlet>
<servlet-name>jersey-helloworld-serlvet</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>com.javacodegeeks.enterprise.rest.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-helloworld-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
And my annotated class:
package com.deangrobler.testrest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/hellorest")
public class HelloRest {
@GET
@Produces("text/plain")
public String hello () {
return "Hello there developer!";
}
}
I expect to then being able to hit it at
http://localhost:8080/TestRest/rest/hellorest/
But... I just get that 404 error.. Something I'm missing? I'd appreciate any help :)
Upvotes: 0
Views: 1374
Reputation: 21
You seem to be using Jersey 2.0 version. So in your web.xml you need to change the servlet part.
<servlet>
<servlet-name>JSON RESTful Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.samplerest.java</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Remember to change the param-value to the name of the package where you have your java code.
This will help :- 404 error with Jersey REST Service on Tomcat
Upvotes: 0
Reputation: 69440
I think the name of your war is TestRest-0.0.1-SNAPSHOT
. So you have to use the url: http://localhost:8080/TestRest-0.0.1-SNAPSHOT/rest/hellorest/
Upvotes: 0
Reputation: 3486
Param com.sun.jersey.config.property.packages is meant to denote packages containing your REST classes. So param-value should be com.deangrobler.testrest.
Check documentation for ServletContainer.
Upvotes: 1