Reputation:
I am working on a simple RESTful Api using Jersey 2.10 and running on Tomcat 8.
this is my Application class :
package com.manager.api.application;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath ("api")
public class Application extends ResourceConfig {
public Application () {
packages ("com.manager.api.resources");
}
}
My Resources package contains :
an interface : Resources.java
package com.manager.api.resources;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public interface Resources {
@GET
@Produces (MediaType.APPLICATION_JSON)
public JsonObject getList ();
@GET
@Path ("value=/{id}")
@Produces (MediaType.APPLICATION_JSON)
public JsonObject get (String id);
@POST
@Path ("value=/{data}")
@Consumes (MediaType.APPLICATION_JSON)
public void post (JsonObject data);
@PUT
@Path ("value=/{data}")
@Consumes (MediaType.APPLICATION_JSON)
public void put (JsonObject data);
@DELETE
@Path ("value=/{id}")
public void delete (String id);
}
An abstract class : ResourcesImpl.java which implements the Resources interface
package com.manager.api.resources;
public abstract class ResourcesImpl implements Resources {
}
And finally a resource class which extends ResourcesImpl.java : UserResources.java
package com.manager.api.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.manager.api.dao.UserDao;
@Path ("value=/users")
public class UserResources extends ResourcesImpl {
private UserDao user = new UserDao ();
@GET
@Path ("value=/test")
@Produces (MediaType.TEXT_PLAIN)
public String Test () {
return "Test";
}
@Override
public JsonObject getList () {
return user.getList ();
}
@Override
public JsonObject get (String id) {
return user.get (id);
}
@Override
public void post (JsonObject data) {
user.post (data);
}
@Override
public void put (JsonObject data) {
user.put (data);
}
@Override
public void delete(String id) {
user.delete (id);
}
}
and my web.xml contains only a <display-name>
Tag :
<?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">
<display-name>manager-api</display-name>
</web-app>
I run my project successfully but I get 2 problems :
The following warnings have been detected: WARNING: A HTTP GET method, public JsonObject com.manager.api.resources.UserResources.get(java.lang.String), should not consume any entity. : Which I find strange since I put @Produces (MediaType.APPLICATION_JSON)
and didn't put a @Consumes
for this method.
Finally the big problem is the 404 error I get when I type : http://localhost:8080/manager-api/api/users/test
when it should print Test
as text.
Do you have any idea about what is the reason behind those 2 errors ? Thank you.
Upvotes: 1
Views: 4209
Reputation: 2242
Make sure your pom.xml has servlet 3.0 and not 2.0. Also try giving an absolute path instead of a relative one in your @ApplicationPath annotation (such as "/api"). Also maybe your package declaration is incorrect for the resourceconfig?
Refer to this if you have more troubles: How to set up JAX-RS Application using annotations only (no web.xml)?
Upvotes: 1