简体   繁体   English

泽西岛HTTP响应返回json值

[英]Jersey HTTP Response return json value

public class Dto {
    private boolean flag;
    private String code;
    private String message;
    private Object data;
    ......
}

It is a generic return format. 这是一种通用的返回格式。

@GET
@Path("/test2")
public Response appLogin2() {
    Dto dto = new Dto();
    Device device = new Device();
    device.setAreaName("NewYork");
    dto.setData(device);
    dto.setFlag(true);
    Response res = Response.status(Status.OK).entity(dto).build();
    return res;
}

But now its return value is 但是现在它的返回值是

{
    "data": "com.gmi.its.itsmain.model.Device@7067fd1f",
    "flag": true
}

How can I get that return value 我如何获得该返回值

{
    "data": {
            "areaName": "NewYork"
        },
    "flag": true
}

I user springboot can get that return value. 我的用户springboot可以获取该返回值。

You should specify a class for that data property. 您应该为该data属性指定一个类。 Currently, it is being read as an Object, which means that the serializer doesn't know what to expect about it. 当前,它被作为对象读取,这意味着串行器不知道对此会有什么期望。 The only thing that the serializer can do is calling the Object's toString() method, which outputs className@hashCode . 序列化程序唯一可以做的就是调用对象的toString()方法,该方法输出className@hashCode

Try something like this: 尝试这样的事情:

public class Dto {
  private boolean flag;
  private String code;
  private String message;
  private Device data;
  ......
}

If you are not sure about the class that you will use for the data field, you should create an interface that all these possible classes extend from. 如果不确定要用于data字段的类,则应创建一个接口,所有这些可能的类都将从该接口扩展。

String jsonString = JSON.toJSONString(dto); 字符串jsonString = JSON.toJSONString(dto); return Response.status(Status.OK).entity(jsonString).build(); 返回Response.status(Status.OK).entity(jsonString).build();

I did it this way now 我现在是这样做的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM