我正在尝试从班级启动 Jetty 服务器。当我尝试从 Eclipse 运行它时,它工作正常。如果我将它嵌入到 JAR 中,码头服务器会启动,但是当我发出请求时,它会返回 500 代码响应。这是我的课:
@GET
@Path("test")
@Produces(MediaType.APPLICATION_JSON)
public String echo(@QueryParam("testParam")String test){
return test;
}
private Server server;
public synchronized void start(int port) throws Exception {
if (server != null) {
throw new IllegalStateException("Server is already running");
}
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
Map<String,Object> initMap = new HashMap<String, Object>();
initMap.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
initMap.put("com.sun.jersey.config.property.packages", "the.class.package");
context.addServlet(new ServletHolder(new ServletContainer(new PackagesResourceConfig(initMap))), "/*");
this.server = new Server(port);
this.server.setHandler(context);
this.server.start();
}
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.out.println("AnnotatorServer <port>");
System.exit(-1);
}
JettyServer server = new JettyServer();
server.start(Integer.parseInt(args[0]));
}
当我尝试在 JAR 中启动它时,服务器会启动,但是当我访问该方法时,会出现以下异常:
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class java.lang.String, and Java type class java.lang.String, and MIME media type application/octet-stream was not found
你知道为什么会这样吗?谢谢!