我正在尝试使用 rest easy 来实现一个 rest 端点。这是一个 GET 端点,
@Controller
@Path("/api")
public class TestController {
private static final Log LOG = LogFactory.getLog(TestController .class);
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response getTest() {
LOG.info(" inside test");
Response r = null;
try {
Test test = new Test();
test.setId(1L);
test.setName("test");
test.setAge("20");
r = Response.ok(test).build();
} catch (Exception e){
LOG.error(e);
}
return r;
}
}
下面是我试图返回的实体类
@XmlRootElement
public class Test {
@XmlElement(name = "id")
private Long id;
@XmlElement(name = "name")
private String name;
@XmlElement(name = "age")
private String age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
从休息客户端调用端点时出现以下错误。
Could not find MessageBodyWriter for response object of type: com.package.Test of media type: application/json
这些是我添加的一些依赖项,我相信它们对此会很有用。
httpclient-4.0.3.jar
httpcore-4.0.1.jar
jackson-core-asl-1.6.3.jar
jackson-jaxrs-1.9.13.jar
jackson-jaxrs-json-provider-2.2.1.jar
jackson-mapper-asl-1.6.3.jar
jackson-xc-1.6.3.jar
jaxrs-api-3.0.12.Final.jar
jboss-logging-3.3.1.Final.jar
jcip-annotations-1.0.jar
resteasy-jaxb-provider-3.1.0.Final.jar
resteasy-jettison-provider-2.3.1.GA.jar
resteasy-spring-2.2.1.GA.jar
scannotation-1.0.3.jar
有谁知道为什么会出现这种错误。端点能够返回一个纯字符串作为响应。