嗨,我正在尝试使用 Jersey 从简单休息的方法中返回 Json
HelloWorldService.class
@Path("/hello")
public class HelloWorldService {
@GET
@Path("/empdetail/{id}")
@Produces(MediaType.APPLICATION_JSON)
public EmpDetailsVo getEmpDetails(@PathParam("id") String id){
EmpDetailsVo details = new EmpDetailsVo();
details.empId="1202";
details.empName="Akhi";
details.empAddress="123 Main St. Newark PA 19121";
return details;
}
}
EmpDetailsVo
类具有 empId
、name
和 address
的 getter 和 setter。
当我尝试运行此网址时:
http://localhost:8080/jerseyRest/rest/hello/empdetail/1202
我得到 Http 状态 500
。
在控制台上我看到一个错误:
SEVERE: A message body writer for Java class jerseyRest.EmpDetailsVo, and Java type class jerseyRest.EmpDetailsVo, and MIME media type application/json was not found
还有
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class jerseyRest.EmpDetailsVo, and Java type class jerseyRest.EmpDetailsVo, and MIME media type application/json was not found
有人可以帮忙吗?
最佳答案
您需要告诉 Jersey 如何将 EmpDetailsVo
类型的对象编码和解码到 JSON。
检查this tutorial有关如何做到这一点的示例。这是另一个例子using a different approach 。研究通过 web.xml 提供给您的 Web 应用程序的 com.sun.jersey.api.json.POJOMappingFeature
参数的使用情况,这应该可以帮助您实现目标。
关于java - 使用 Jersey 简单Rest返回Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22948173/