Restful Jetty HTTP错误404访问未找到
问题描述:
这是我的网址; http://localhost:2222/test1/test1/home/helloRestful Jetty HTTP错误404访问未找到
此网址导致下面的错误。 HTTP错误404 访问/ test1/test1/home/hello问题。原因: 未找到下面
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!";
}
}
我在做什么错误
及主要的servlet起动器和资源类?
编辑:我认为问题是在“包”部分,但我不知道如何配置它。 谢谢
答
您创建了ServletContextHandler
但未将其添加到服务器。
添加...
HandlerList handlers = new HandlerList();
handlers.addHandler(context);
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
让我们继续评论民事请。 –