如果从JAR启动,Jetty + Jersey嵌入式服务器无法正常工作

时间:2012-10-17 11:56:38

标签: java eclipse jar jetty jersey

我正在尝试从类中启动Jetty服务器。当我尝试从eclipse运行它时它工作正常。如果我将它嵌入到JAR中Jetty服务器启动,但是当我发出请求时它返回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

你知道为什么会这样吗?谢谢!

1 个答案:

答案 0 :(得分:0)

问题出在POM文件上。依赖性是覆盖Meta-Inf / service中的一些文件。找到Jersey exception only thrown when depencencies assembled into a single jar

的解决方案