JsonIgnore 注释似乎对我不起作用。任何想法为什么?
public class JsonTest implements Serializable {
@JsonIgnore
private static JsonTest instance = null;
@JsonIgnore
private transient Set<String> set = new CopyOnWriteArraySet<String>();
private JsonTest() {}
@JsonIgnore
public static JsonTest getInstance() {
if (instance == null)
instance = new JsonTest();
return instance;
}
public void setSet(Set<String> set) {
this.set = set;
}
@JsonIgnore
public Set<String> getSet() {
return set;
}
public String toString() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
try {
return mapper.writeValueAsString(this);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
set.add("test");
JsonTest.getInstance().setSet(set);
System.out.println(JsonTest.getInstance().toString());
}
}
输出:{"set":["test"]}