注解名稱:@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(); } } }