java jackson 忽略不存在的属性字段 和 按照属性名转json


@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
该注解的作用是:使用属性名称来转json。 防止不规范的属性报错,比如 app_id
 
@JsonIgnoreProperties(ignoreUnknown = true)
该注解的作用是:在进行json转对象是,防止传入的json串包含不存在的属性导致报错。

案例:

 1 @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
public class UnifiedPayOrderResponse { private String code; private String message; private String main_order_code; private String app_id; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public String getMain_order_code() {
return main_order_code;
} public void setMain_order_code(String main_order_code) {
this.main_order_code = main_order_code;
} public String getApp_id() {
return app_id;
} public void setApp_id(String app_id) {
this.app_id = app_id;
} }
上一篇:DNS测试工具的使用(了解)


下一篇:(转)C# .net微信开发,开发认证,关注触发消息,自动应答,事件响应,自定义菜单