404 Error in REST-Webservice example with Jersey and Jackson
I am trying to create a simple Java based REST web service using JSON. I also use Maven for dependency management.
Since I am new to this, I followed closely the good 5 step guide at http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/ , however I don’t know, t get this simple example to run.
Maven worked fine and all required dependencies were downloaded and integrated into the project (the libraries are contained in the deployed .war file). The only problem is that when I want to open the url given in the tutorial (localhost: 8080 / RESTfulExample / rest / json / metallica / get) after deploying the project, I get a 404 error without further information.
Edit:
Here is my pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<finalName>RESTfulExample</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
As I said, the build went fine and the dependencies were loaded and integrated into the project.
Now, here is the content of my web.xml Refurbished web application
<servlet>
<servlet-name>jersey-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.mkyong.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Alongside this, I am using the JSON-Service class and the Track model class. My JSON service looks like this:
@Path("/json/metallica")
public class JSONService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON() {
Track track = new Track();
track.setTitle("Enter Sandman");
track.setSinger("Metallica");
return track;
}
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(Track track) {
String result = "Track saved : " + track;
return Response.status(201).entity(result).build();
}
}
I'm not sure how to check for server side exceptions, and any hint of this would be appreciated as well ... :-)
source to share