简体   繁体   English

Restful Jetty HTTP ERROR 404 访问未找到

[英]Restful Jetty HTTP ERROR 404 accessing not found

Here is my url;这是我的网址; http://localhost:2222/test1/test1/home/hello http://localhost:2222/test1/test1/home/hello

This url causes the error below.此网址导致以下错误。 HTTP ERROR 404 Problem accessing /test1/test1/home/hello. HTTP 错误 404 访问 /test1/test1/home/hello 时出现问题。 Reason: Not Found原因:未找到

And main servlet starter and resource class below以及下面的主要 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();
    }

My Resource我的资源

@Path("/home")
public class Resources {

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

What am i doing wrong?我究竟做错了什么?

Edit:I think problem is in "packages" part but i dont know how to configure it.编辑:我认为问题出在“包”部分,但我不知道如何配置它。 Thanks谢谢

You created a ServletContextHandler but didn't add it to the server.您创建了一个ServletContextHandler但没有将它添加到服务器。

Add ...添加 ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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