ObjectMapper限制

时间:2017-01-08 04:22:11

标签: java objectmapper

ObjectMapper是否应该与只有私有成员,没有构造函数和没有getter / setter的类一起工作?

我试过这个,但它没有解决问题。

mapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);

1 个答案:

答案 0 :(得分:0)

http://www.baeldung.com/jackson-field-serializable-deserializable-or-not

static class MyDtoAccessLevel {
    private String stringValue = "hidden";
}

public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    MyDtoAccessLevel dtoObject = new MyDtoAccessLevel();
    System.out.println(mapper.writeValueAsString(dtoObject));
    //prints {"stringValue":"hidden"}
}