简体   繁体   中英

tomcat 7 - @GET method with @ApplicationPath - status 404 error

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??

@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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM