嵌入Jetty 9.0和RestEasy 3.0不断抛出NoSuchMethodError [英] Jetty 9.0 embedded and RestEasy 3.0 keeps throwing NoSuchMethodError

查看:154
本文介绍了嵌入Jetty 9.0和RestEasy 3.0不断抛出NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我有了一个构建非常简单的Web应用程序的想法,该应用程序将由REST后端提供支持.因为我想要一个非常轻巧的服务器,所以我开始研究Jetty.而且由于我想尝试除Jersey之外的另一个JAX-RS实现,因此我研究了RestEasy.我认为这2易于实现.我错了...

Today I had the idea to build a very simple web application, which would be powered by a REST backend. Since I wanted a very lightweight server I started looking at Jetty. And since I wanted to try another JAX-RS implementation than Jersey I looked at RestEasy. I thought those 2 would be easy to implement. I was wrong...

我导入了基本的Jetty服务器和servlet依赖关系,因为我认为这是基本(仅REST)Jetty服务器的唯一服务器要求(我还尝试使用webapp依赖关系;这也产生了相同的错误).

I imported the basic Jetty server and servlet dependencies since I thought that were the only server requirements for a basic (REST only) Jetty server (I alto tried to use the webapp dependency; this gave the same errors).

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>9.0.0.RC0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlet</artifactId>
    <version>9.0.0.RC0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlets</artifactId>
    <version>9.0.0.RC0</version>
    <scope>compile</scope>
</dependency>

然后,我导入了基本的RestEasy依赖项.

Then I imported the basic RestEasy dependencies.

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>3.0.1.Final</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxb-provider</artifactId>
    <version>3.0.1.Final</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>async-http-servlet-3.0</artifactId>
    <version>3.0.1.Final</version>
    <scope>compile</scope>
</dependency>

主要方法:

public class ExampleActivator {
    public static void main(String args[]) throws Exception {
        Server server = new Server(8080);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        context.setContextPath("/");
        ServletHolder h = new ServletHolder(new HttpServlet30Dispatcher());
        h.setInitParameter("javax.ws.rs.Application", "packages.ExampleResources");
        context.addServlet(h, "/*");
        server.setHandler(context);
        server.start();
        server.join();
    }
}

ExampleResources:

ExampleResources:

public class ExampleResources extends Application {

    private static final ImmutableSet services = ImmutableSet.of(
            ExampleResourceImpl.class
    );

    @Override
    public Set<Class<?>> getClasses() {
        return services;
    }
}

ExampleResourceImpl:

ExampleResourceImpl:

@Path("activities")
public class ExampleResourceImpl {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getAll() {
        return "Hello World";
    }
}

当我进入webapge时,得到以下跟踪信息:

When I go to the webapge I get the following trace:

250 [main] INFO org.eclipse.jetty.server.Server - jetty-9.0.0.RC0
911 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Deploying javax.ws.rs.core.Application: class packages.ExampleResources
939 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class resource packages.ExampleResourceImpl from Application class packages.ExampleResources
1005 [main] INFO org.eclipse.jetty.server.handler.ContextHandler - started o.e.j.s.ServletContextHandler@702ab48{/,null,AVAILABLE}
1037 [main] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@465ed596{HTTP/1.1}{0.0.0.0:8080}
6315 [qtp84346444-13] WARN org.eclipse.jetty.servlet.ServletHandler - Error for /activities
java.lang.NoSuchMethodError: org.jboss.resteasy.specimpl.BuiltResponse.getHeaders()Ljavax/ws/rs/core/MultivaluedMap;
    at org.jboss.resteasy.core.ServerResponseWriter.setDefaultContentType(ServerResponseWriter.java:195)
    at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:46)
    at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:411)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:376)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:671)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:448)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1070)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:375)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1004)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:449)
    at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:246)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:265)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:589)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:520)
    at java.lang.Thread.run(Thread.java:722)

通常这意味着我忘记了依赖性,但是我真的不知道发生了什么.

Normally this would mean that I forgot a dependency, however I really have no clue what's going on.

推荐答案

我偶然发现了同一问题.解决方案是将jaxrs-api明确添加为依赖项列表中的第一个依赖项.

I just stumbled upon the same issue. The solution was to add the jaxrs-api explicitly as the first dependency in the list of dependencies.

<dependencies>
  <!-- jaxrs-api is the very first dependency -->
  <dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>jaxrs-api</artifactId>
    <version>3.0.4.Final</version>
  </dependency>
  <!-- here come the other depdendencies -->
</dependencies> 

Jackson使用的jsr311-api提供与BuiltResponse类所使用的相同的Response类.不幸的是,此Response不包含getHeaders()方法.因此,即使解决了依赖关系,它也使用了错误的类.如果杰克逊的依赖关系以某种方式出现在Resteasy之前,则将引发上述异常.

Jackson uses the jsr311-api that provides the same Response class that is used by the BuiltResponse class. Unfortunately, this Response does not contain the getHeaders() method. So even though the dependency is resolved, it is using a wrong class. If somehow the Jackson dependency gets before the Resteasy one, then the exception above will be raised.

这篇关于嵌入Jetty 9.0和RestEasy 3.0不断抛出NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆