我需要定义一个ObjectMapper全局序列化/反序列化对象,我只需要带有公共getter的字段,setter将被序列化。
如果存在以下内容可能会很棒:
objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.PUBLIC_ONLY)
.withIsGetterVisibility(JsonAutoDetect.Visibility.PUBLIC_ONLY)
.withSetterVisibility(JsonAutoDetect.Visibility.PUBLIC_ONLY)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
但是"和"而不是"或"。
有任何想法吗?
PS:我可以用注释来做,但我需要全局做。
提前致谢。
答案 0 :(得分:1)
看到问题的成功,我被迫自己寻求解决方案。它不是一个理想的解决方案,但它适合我。
private void configureObjectMapper(MappingJackson2HttpMessageConverter converter) {
final ObjectMapper objectMapper = new ObjectMapper();
// Only serialize fields with both public accessors
objectMapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true);
// If a field is not found, then ignore it and continue processing, with no fail
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
converter.setObjectMapper(objectMapper);
}
问候!