当我尝试使用包javax.ws.rs.core中的Response.readEntity()
方法将响应映射到实体时,出现此异常。在泽西岛测试中。我不确定我在做什么错?这个错误可能很明显,但是对于JAX-RS和Jackson来说,我还是很陌生。
例外:
javax.ws.rs.ProcessingException: Error reading entity from input stream.
org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:112)
at example.src.ExampleTest.putExample(ExampleTest.java:261)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot construct instance of javax.ws.rs.core.Link (although at least one Creator exists):
cannot deserialize from Object value (no delegate- or property-based Creator)at
[Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 242]
(through reference chain: example.src.ExampleResponse["links"]->java.util.ArrayList[0])
我有一个响应班:
@JsonPropertyOrder({"name"});
public class ExampleResponse() {
private String name;
private List<Link> links = new ArrayList<>();
public List<Link> getLinks() {
return links;
}
public void addLink(String url, String rel) {
links.add(Link.fromUri(url)
.rel(rel)
.build());
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public static ExampleResponse instance(String url) {
ExampleResponse er = new ExampleResponse();
er.setName("someName");
er.addLink(url, "relation");
return er;
}
}
然后我有一个资源:
@Path("/1/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ExampleResouce() {
private String uri = someInitMethod();
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response putExample() {
return Response.status(Status.OK)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(ExampleResponse.instance(uri))
.build();
}
}
最后是在readEntity上失败的测试:
@Test
public void testPut() {
ExampleResponse toPut = new ExampleResponse();
toPut.setName("New_Name");
final Response response = target("/1/").request()
.put(Entity.json(toPut));
assertEquals(Status.OK.getStatusCode(), response.getStatus());
ExampleResponse er = response.readEntity(ExampleResponse.class);
}