这是我的网址; 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!";
}
}
我做错了什么?
编辑:我认为问题出在“软件包”部分,但我不知道如何配置它。 感谢
答案 0 :(得分:0)
您创建了ServletContextHandler
但未将其添加到服务器。
添加...
HandlerList handlers = new HandlerList();
handlers.addHandler(context);
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);