java - 使用 Jersey 简单Rest返回Json

标签 java json rest jersey

嗨,我正在尝试使用 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 类具有 empIdnameaddress 的 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/

相关文章:

java - 支持 Hibernate 3.6.1 并升级到 5

java - Selenium typeKeys 从正在键入的字符串中删除点

javascript - 使用 $httpBackend + $resource 进行 Angular karma 单元测试,无需使用模块引导

python - 在 callbackURL 端点从 Google Glass 获取 "REPLY"通知

java - 如何在Hibernate中启用二级缓存

java - 在SWT中测量CTabItems的字体大小?

java - 为什么JSON文档没有被完全消费?

json - 在 Swift 中解析 PHP Web 服务 Replt

ios - 如何在 Swift 中的 NSJSONSerialization.dataWithJSONObject 之后获得可读的 JSON

rest - Linkedin rest api 搜索有名字的人