Reputation: 109
i want to run my project on tomcat using endpoint path folowing are my two java files
this is my app class
package app;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/t")
public class App extends Application{
}
this is endpoint class
package controllers;
import java.util.List;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import dao.IEntityDAO;
import daoimpl.EntityDAOImpl;
import dto.Contacts;
import view.ContactView;
@ApplicationPath("/t1")
public class ContactController {
@Path("/hi")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getpassword()
{
return "Hiii";
}
@Path("/t2")
@GET()
@Produces(MediaType.APPLICATION_JSON)
public List<ContactView> getallEntity(){
IEntityDAO obj = new EntityDAOImpl();
return obj.getallEntity();
}
}
my tomcat 7 is running But when i run it on tomcat by following path
http://localhost:8006/ContactApp/t/t1/hi
it showing following error
HTTP Status 404 - /ContactApp/t/t1/hi
type Status report
message /ContactApp/t/t1/hi
description The requested resource is not available.
Apache Tomcat/7.0.47
anyone can help??
Upvotes: 1
Views: 913
Reputation: 131346
@ApplicationPath
may only be applied to a subclass of Application :
Identifies the application path that serves as the base URI for all resource URIs provided by Path. May only be applied to a subclass of Application.
For this one :
@ApplicationPath("/t")
public class App extends Application{
}
It is fine.
But it is not the case for ContactController
that should not declared with @ApplicationPath
:
@ApplicationPath("/t1")
public class ContactController {
but with @Path
(without leading slash):
@Path("t1")
public class ContactController {
Extract of Path javadoc :
Identifies the URI path that a resource class or class method will serve requests for. .... Paths are relative. For an annotated class the base URI is the application path, see ApplicationPath.
At last, you should remove the leading slash for the @Path
of your REST methods : @Path("/t1")
It is not required as the specification of Path
explains that leading /
are ignored and that base URI are handled as if a /
was added.
For the purposes of absolutizing a path against the base URI , a leading '/' in a path is ignored and base URIs are treated as if they ended in '/'
So these :
@Path("/hi")
...
@Path("/t2")
should be replaced by :
@Path("hi")
...
@Path("t2")
Upvotes: 3