例如我的bean中有以下4个字段
private int Ret;
private String Msg;
private Object Data;
private String Sig;
引入依赖
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.3</version>
</dependency>
在返回的json里只会显示:{"ret":"xx","msg":"xx","data":"xx","sig":"xx"}
* 使用下面方法优化完美解决该问题。
* 返回json会变成:[{"Ret":"xx","Msg":"xx","Data":"xx","Sig":"Sig"}](www.baidu.com),完美解决问题!
* 大小会变成小写,特殊符号开头的字段都不会显示,其原因是因为springboot在进行序列化和反序列时对字段进行了处理。
解决方案是:import com.fasterxml.jackson.annotation.JsonProperty;
* 在get方法上加上该注解@JsonIgnore
* 在字段上加上该注解@JsonProperty
package com.hlht.evcs.bean;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* 查询数据库返回
*/
public class HlhtRespDB {
@JsonProperty(value = "Ret")
private int Ret;
@JsonProperty(value = "Msg")
private String Msg;
@JsonProperty(value = "Data")
private Object Data;
@JsonProperty(value = "Sig")
private String Sig;
public HlhtRespDB() {
}
public HlhtRespDB(int Ret, String Msg, Object Data, String Sig) {
this.Ret = Ret;
this.Msg = Msg;
this.Data = Data;
this.Sig = Sig;
}
@JsonIgnore
public int getRet() {
return Ret;
}
public void setRet(int ret) {
Ret = ret;
}
@JsonIgnore
public String getMsg() {
return Msg;
}
public void setMsg(String msg) {
Msg = msg;
}
@JsonIgnore
public Object getData() {
return Data;
}
public void setData(Object data) {
Data = data;
}
@JsonIgnore
public String getSig() {
return Sig;
}
public void setSig(String sig) {
Sig = sig;
}
}