Stack Exchange
How to test jersey restful api using jetty?
When I run test using grizzly2 follow steps here . Everything works fine. Then I tried jersey-test-framework-provider-jetty, exception happend:
  MessageBodyReader not found for media type=text/html;charset=iso-8859-1, type=class com.example.resource.GenericPair, genericType=com.example.resource.GenericPair<java.lang.Integer, java.lang.String>.
  HTTP 500 Server Error.

Response is:
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 500 </title>
</head>
<body>
<h2>HTTP ERROR: 500</h2>
<p>Problem accessing /test. Reason:
<pre>    java.lang.NoSuchMethodError: org.eclipse.jetty.server.Response.getResponse(Ljavax/servlet/http/HttpServletResponse;)Lorg/eclipse/jetty/server/Response;</pre></p>
<hr /><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.10.v20160621</a><hr/>
</body>
</html>

May be It need some more configure using jetty. Here is my configure
@Override
protected Application configure() {
    return new ResourceConfig(TestApi.class)
            .register(JacksonFeature.class)
            .register(JacksonJsonProvider.class)
            .packages("com.example.api");
}

UPDATE
--------pom.xml---------
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-jetty</artifactId>
            <version>${jersey.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <jersey.version>2.23.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jetty.version>9.3.10.v20160621</jetty.version>
    </properties>

-------TestApiTest.java--------
public class TestApiTest extends JerseyTest {

    @Override
    protected Application configure() {
        return new ResourceConfig(TestApi.class)
                .register(JacksonFeature.class)
                .register(JacksonJsonProvider.class)
                .packages("com.example.api");
    }

    @Override
    protected URI getBaseUri() {
        return URI.create(super.getBaseUri().toString() + "");
    }

    @Override
    protected void configureClient(ClientConfig config) {
        super.configureClient(config);
    }

    @Test
    public void testIndex() throws Exception {
        Response response = target().path("test").request().get();
        String responseMsg = response.readEntity(String.class);
        assertEquals("this is test api", responseMsg);
    }

    @Test
    public void testList() throws Exception {
        Response response = target().path("test/list").request().get();
        List<Pair> pairs = response.readEntity(new GenericType<List<Pair>>(){});//从response中读取泛型集合
        assertEquals(3, pairs.size());
    }
}

-----------ApiTest.java------------
@Path("test")
public class TestApi {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String index() {
        System.out.println("-------------");
        return "this is test api";
    }

    @GET
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Pair> list() {
        List<Pair> pairs = new ArrayList<Pair>();
        pairs.add(new Pair(1, "pair1"));
        pairs.add(new Pair(2, "pair2"));
        pairs.add(new Pair(3, "pair3"));
        return pairs;
    }
}


You probably have a jetty transitive version mix-match. If you already have Jetty dependencies for your main project, you may want to just exclude all of the ones that come with the test framework dependency. You can see here the three that Jersey uses

I am afraid not, see UPDATE please.

I mean there is no "transitive version mix-match".

Are you sure?.

Oh, you are a genius. Finally, I got it. I change jetty.version to 9.2.14.v20151106, then everything is fine. But, one more question, there are so many different version with jersey, jackson, jetty etc. How could I know which version is the right one? Should I always use the version that jersey depend on?

In some cases you can use a different version, and sometimes you can't. It all depends. Most of the time you can use newer versions, as long as the dependency is backwards compatible. But you just need to make sure that you exclude all dependencies that Jersey uses, and explicitly declare the new version. Like in your case with Jackson. You are using a newer version than Jersey uses. But you have not excluded them from the Jersey dependency. So it's possible that different version of transitive dependencies get pulled in. Which may give you a similar error.

Whether or not to use a different version really comes down to if you need features in a different version. In which case you just need to play around with excluding and redeclaring transitives and hope it works
