简体   繁体   中英

Jersey Jetty Embedded Won't Read Path Annotation, Error 404

Please help me guys I'm going insane here..

So I was trying to use jersey (2.22.1) in an embedded jetty (9.2.14.v20151106), with the very simple main app like this:

public class MainApp{
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);

        ServletHolder sh = new ServletHolder(ServletContainer.class);
            sh.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "etc.mypackage");
            sh.setInitParameter(ServerProperties.PROVIDER_SCANNING_RECURSIVE, "true");
            sh.setInitParameter(ServerProperties.TRACING, "ALL");
            sh.setInitParameter("jersey.config.server.tracing", "ALL");
            sh.setInitOrder(1);
            sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
        context.addServlet(sh, "/");
        server.start();
        server.join();
    }
}

One of the thing that is not working here is that the jersey tracing doesn't seem to be working even if I set it in the parameter.

And I have 2 servlets at etc.mypackage.servlets :

@Path("/")
public class TestServlet {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String testString){ return "TestServlet.class; }

    @Path("test1")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getString(){
        String test = "TestServlet.class second method called";
        return test;
    }
}

and the other with identical functions but with different path and debug string :

@Path("/2")
public class TestServlet2 {
    @Path("/test1")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String testString){ return "TestServlet2.class; }

    @Path("test2")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getString(){
        String test = "TestServlet2.class second method called";
        return test;
    }
}

These codes run and if called it in http://localhost:8080/ it will return a plain text TestClass.class from the first method of that class, but if I added any more path in the link, it will just return the same string, and no way I could invoke for it to display the second method of TestServlet class or TestServlet2 .

But if changed the path from TestServlet from @Path("/") into anything other than that, it will just return error 404 for any URL including the http://localhost:8080/ . I tried everything that I can think of the causes to but no avail, and it's so frustrating. Am I missing something here?

Thanks...

Here context.addServlet(sh, "/"); you are telling what path to match for access to the servlet. So the only access point you have is / . if you want access to "anything after / " , you should add the wildcard * . So just change it to .addServlet(sh, "/*");

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