【问题标题】:Json response value is not setting to the Bean classJson 响应值未设置为 Bean 类
【发布时间】:2016-07-13 14:29:49
【问题描述】:

当我做休息课时,我收到了以下回复。

回复:

     {
            "_index": "a_index",
"total":9,
            "_type": "e",
            "_id": "BSKnamtd_8-egMNvh",
            "_data": 2.076404564,
            "_secure": {
              "email": "abcd1@gmail.com"
            }
          }

设置此响应。我已经创建了 pojo 类,如图所示。

public class data implements Serializable {

    private static final long serialVersionUID = 644188100766481108L;
    private String _index;
    private Integer total;
    private String _type;
    private String _id;
    private Double _data;
    private Source _secure;

    public String getIndex() {
        return _index;
    }

    public void setIndex(String _index) {
        this._index = _index;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public String getType() {
        return _type;
    }

    public void setType(String _type) {
        this._type = _type;
    }

    public String getId() {
        return _id;
    }

    public void setId(String _id) {
        this._id = _id;
    }

    public Double getData() {
        return _data;
    }

    public void setData(Double _data) {
        this._data = _data;
    }

    public Source getSecure() {
        return _secure;
    }

    public void setSecure(Source _secure) {
        this._secure = _secure;

    }

}

当我点击 restClient 调用时,我只得到“总”值,剩余值变为空。 “总”变量没有下划线(“”)剩余变量有“”,所以我面临问题..?

请帮助解决这个问题。

【问题讨论】:

  • 到目前为止有什么运气吗?

标签: java json spring rest resttemplate


【解决方案1】:

Json 对象是

...
"_index": "a_index",
"total":9,
...

这两个属性的属性是:

 public void setIndex(String _index) {
        this._index = _index;
    }

public void setTotal(Integer total) {
        this.total = total;
    }

如您所见,Json _index 属性java 属性index (setIndex)

index_index 不同,所以当 Json 对象被映射时,它会为 null,因为它找不到属性 _index。

另一方面,Json 总属性java 属性total (setTotal)

在这种情况下,Json 和 Java 中的属性具有相同的名称,因此它会加载值。

【讨论】:

    【解决方案2】:

    Java 类属性名称对对象映射器不可见(它是private)。在您的情况下,重要的是 getter/setter 名称。它用于将 JSON 对象属性名称映射到 Java 类 (POJO)。

    你有两个我能想到的选择。肮脏的一种是将setter名称更改为set_index ()set_type()等,以便它们对应于JSON属性名称,或者您可以这样做:

    1. 将 Java 类属性和方法的名称更改为标准约定,以便代码美观且可读。
    2. 使用 @JsonProperty 注释属性以表达 JSON 和 Java 对象之间的默认映射不同。

    来自注释类型 JsonProperty Java 文档:

    默认值(“”)表示使用字段名作为 属性名称没有任何修改,但可以指定为 非空值指定不同的名称。属性名称指 外部使用的名称,作为 JSON 对象中的字段名称。

    带注释的示例:

    public class test {
    
        @JsonProperty("_index")
        private String index;
    
        private Integer total;
    
        @JsonProperty("_type")
        private String type;
    
        public String getIndex() { return index; }
        public void setIndex(String index){ this.index = index; }
    
        public Integer getTotal() { ... }
        public void setTotal(Integer total) { ... }
    
        public String getType() { ... }
        public void setType(String type) { ... }
        ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-08
      • 2011-02-20
      • 2019-12-18
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      相关资源
      最近更新 更多