当我使用grizzly2运行测试时,请按照步骤here进行操作。一切正常。然后我尝试了jersey-test-framework-provider-jetty,异常发生:
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.
回应是:
<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>
可能需要使用jetty进行更多配置。这是我的配置
@Override
protected Application configure() {
return new ResourceConfig(TestApi.class)
.register(JacksonFeature.class)
.register(JacksonJsonProvider.class)
.packages("com.example.api");
}
-------- --------- 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;
}
}