简体   繁体   English

有没有一种方法,当我使用jackson&spring mvc将json映射到pojo时,我不需要为布尔字段添加@JsonProperties

[英]Is there a way that I donot need add @JsonProperties for boolean field when mapping json to pojo using jackson&spring mvc

I cannot find a way to fix this issue, but... maybe it's not an issue. 我找不到解决此问题的方法,但是...也许这不是问题。

I use Extjs as the front-end and Spring MVC as backend, the Ajax request looks like: 我将Extjs用作前端,将Spring MVC用作后端,Ajax请求如下所示:

{"isOk": true}

The Mapping DTO is: 映射DTO为:

public class TestDTO implements Serializable {

    private static final long serialVersionUID = -6074462313103219627L;

    private Boolean isOK;
    public Boolean isOk(){...}
    Public void setOk(Boolean isOk){...}
}

The get/set method be generated by intellij idea, as you can imagine that jackson works fine if I add @JsonProperty("isOk") under the "setOk" method. get / set方法是由intellij idea生成的,可以想象如果我在“ setOk”方法下添加@JsonProperty(“ isOk”)可以使jackson正常工作。

But I have a lot of ***DTO objects, so is there a convenient method to reslove this issue? 但是我有很多*** DTO对象,因此有解决此问题的简便方法吗? thanks. 谢谢。


I have checked the "com.fasterxml.jackson.databind.SerializationFeature" class, and didn't find any config which like the "compatible_boolean_and_ignore_is_keyword" etc.. 我检查了“ com.fasterxml.jackson.databind.SerializationFeature”类,但未找到任何类似“ compatible_boolean_and_ignore_is_keyword”等的配置。

I didn't test it, but might be helpful for your case: 我没有对其进行测试,但可能会对您的情况有所帮助:

https://stackoverflow.com/a/35088196/677937 https://stackoverflow.com/a/35088196/677937

Basically, try to rename your getter/setter to: 基本上,尝试将您的getter / setter重命名为:

getIsOk / setIsOk

It should then serialize/deserialize json in form of {"isOk": ... } 然后,它应该以{"isOk": ... }形式序列化/反序列化json {"isOk": ... }

It's been some time since I used spring, but if I recall correctly you have to 自从我使用Spring已经有一段时间了,但是如果我没记错的话,你必须

  • annotate the class with @Entity 用@Entity注释类
  • implement the Serializable interface ( class DTO implements Serializable ) 实现Serializable接口( class DTO implements Serializable
  • provide a default constructor 提供默认的构造函数

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

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