@JsonIgnore注解|@JsonProperty

注解名称:@JsonIgnore
作用:在实体类向前台返回数据时用来忽略不想传递给前台的属性或接口。
Eg:Bean实体中会有某些运维字段,在返回信息给前台的时候,当不希望将对应值也一并返回;
  此时可以在对应属性上加上注解JsonIgnore或者,可以在User类上加上注解@JsonIgnoreProperties(value = "{password}")

以下是给出一个小Demo供测试参考:


public class WiBean {

    
    // 忽略参数返回
    @JsonIgnore
    private String names;
    
    // 用于属性上、set/get方法上,该属性序列化后可重命名。
    @JsonProperty(value="val")
    private String values;

    @JsonIgnore
    public String getNames() {
        return names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    @JsonProperty(value="val")
    public String getValues() {
        return values;
    }

    public void setValues(String values) {
        this.values = values;
    }
    
    
}
public class GoControl {

//    @JsonIgnore
//    @JsonProperty
    
    
    public static JSONObject printParam(){
        WiBean bean = new WiBean();
        bean.setNames("key");
        bean.setValues("value");
        
        return JSONObject.fromObject(bean);
    }
    
    public static void main(String[] args) {
        WiBean bean = new WiBean();
        bean.setNames("tan");
        bean.setValues("dalei");
        try {
            System.out.println(new ObjectMapper().writeValueAsString(bean));
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
原文地址:https://www.cnblogs.com/tanjiyuan/p/11058936.html