Java – use Jackson JSON parsing to convert true or false to Boolean values

I'm using the Jackson annotation to parse the JSON response into a POJO object I use Boolean variables in POJO to map the values "true" and "false" from JSON But suddenly, we changed the values in JST to "true" and "false", and resolved the failure of these values

Solution

This is not a real problem, it's basically the way BeanUtils works

For Boolean variables, Jackson removes the name from setter to derive the variable name it expects when grouped into JSON, and adds set to the same derived name to ungroup back into POJO

So Boolean isfootrue; When marshalling to JSON, the end is footorue. When ungrouping it, it will try to call setisfootorue();, This is not true

If you are using the IDE and generate getters / setters, you may notice the generated Boolean isfoo code; Basically ignored, as if the VaR name was just foo:

private boolean isFoo;

public boolean isFoo() {
    return isFoo;
}

public void setFoo(boolean isFoo) {
    this.isFoo= isFoo;
}

Two options are to remove is from the VaR name or add is to the setter name

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>