1
我試圖從類中啓動Jetty服務器。當我嘗試從eclipse運行它時,它工作正常。如果我將它嵌入到JAR中,碼頭服務器就會啓動,但是當我提出請求時,它將返回500代碼響應。這裏是我的課:如果從JAR啓動,Jetty + Jersey嵌入式服務器無法正常工作
@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
你有任何想法爲什麼會發生?謝謝!
你能詳細說明'嵌入JAR'是什麼意思嗎,你把類放在jar嗎?你把jersey jar放在jar嗎? –
我有一個maven項目和程序集使用maven-assembly-plugin的項目,如果你想我可以添加maven pom文件 – pokeRex110