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.
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
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:
the web.xml should look like:
Web request from
projectURL/rest/hello/
will match toHelloWorldService
through@Path("/hello")
and the{any values}
fromprojectURL/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