找不到 Restful Jetty HTTP 错误 404 访问



这是我的网址;http://localhost:2222/test1/test1/home/hello

此网址会导致以下错误。HTTP 错误 404访问/test1/test1/home/hello 时出现问题。原因: 未找到

下面是主 servlet 启动器和资源类

 ResourceConfig config = new ResourceConfig();
config.packages("java"); // this is where my main class and resource resides
    ServletHolder servlet = new ServletHolder(new ServletContainer(config));

    Server server = new Server(2222);
    ServletContextHandler context = new ServletContextHandler(server, "/test1",ServletContextHandler.NO_SESSIONS);
    context.addServlet(servlet,"/test1");
    try
    {
        server.start();
        server.join();
    }
    catch(Exception ex){
        ex.printStackTrace();
        server.destroy();
    }

我的资源

@Path("/home")
public class Resources {
@GET
@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
public String helloWorld() {
    return "Hello, world!";
}
}

我做错了什么?

编辑:I认为问题出在"包"部分,但我不知道如何配置它。谢谢

您创建了一个ServletContextHandler,但未将其添加到服务器。

加。。。

HandlerList handlers = new HandlerList();
handlers.addHandler(context);
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);

最新更新