【问题标题】:Simple Rest to return Json using jerseySimple Rest 使用球衣返回 Json
【发布时间】:2014-05-21 19:21:30
【问题描述】:

您好,我正在尝试使用 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

有人可以帮忙吗。

【问题讨论】:

    标签: java json rest jersey


    【解决方案1】:

    您需要告诉 Jersey 如何将 EmpDetailsVo 类型的对象编组和解组为 JSON。

    查看this tutorial 以获取有关如何执行此操作的示例。这是另一个例子using a different approach。调查通过 web.xml 提供给您的 Web 应用程序的 com.sun.jersey.api.json.POJOMappingFeature 参数的使用情况,这应该可以帮助您实现目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-04
      • 2021-03-18
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多