问题
I'm working on my first RESTful Api with Jersey 2.x and tomcat 8 but when I try to acceed to my Resources I keep getting a 404 errors.
this is my web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<servlet>
<servlet-name>com.pj.api.application.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>com.pj.api.application.Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
My Application class :
@ApplicationPath ("api")
public class Application extends ResourceConfig {
public Application () {
packages ("com.pj.api.resources");
}
}
My Resources class :
@Path ("value=/users")
public class UserResources extends ResourcesImpl {
private UserDao user = new UserDao ();
@Override
public List<Data> getList () {
return user.getList ();
}
@GET
@Path ("value=/test")
public String Test () {
return "{'a':'hey'}";
}
@Override
public Data get (String id) {
return user.get (id);
}
@Override
public void post (Data data) {
user.post (data);
}
@Override
public void put (Data data) {
user.put (data);
}
@Override
public void delete(Data data) {
user.delete (data);
}
}
When deploying the project on Tomcat, and acceeding to the Service through the URL : http://localhost:8080/PJ/api/users/test it gives me a 404 error and Cannot cast org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer to javax.servlet.ServletContainerInitializer
p.s : I do NOT use Maven
What could be the problem here ? Thank you.
回答1:
This certainly looks like a classpath issue. Does your server runtime include some default Jax-rs libraries? I faced a similar issue when trying to deploy my app built on Jersey 2.4 on a server that included jax-rs 1.1 libraries, so I had to rebuild my app on Jersey 1.18. Check your server runtime in Eclipse for any existing libraries
回答2:
Your app differs a lot from mine. I suggest you to take a look at a simple hello world example that you can find here.
As you can see a simple service is:
@Path("/hello")
public class HelloWorldService {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
the web.xml should look like:
<web-app id="WebApp_ID" version="2.4"
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_2_4.xsd">
<display-name>Restful Web Application</display-name>
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Web request from projectURL/rest/hello/ will match to HelloWorldService through @Path("/hello") and the {any values} from projectURL/rest/hello/{any values} will match to parameter annotated with @PathParam.
The URL is http://localhost:8080/<your_project>/rest/hello/Mehdi
You can use this base example as a root base and adapt it to your needs.
Hope to help
来源:https://stackoverflow.com/questions/24144289/404-error-jersey-2-x-on-tomcat-8