Browse Source

Add visibility support to Jackson2ObjectMapperBuilder

Issue: SPR-16411
pull/1828/merge
Eddú Meléndez 7 years ago committed by Sebastien Deleuze
parent
commit
48c3fa9908
  1. 19
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
  2. 30
      spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

19
spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

@ -27,8 +27,10 @@ import java.util.Locale; @@ -27,8 +27,10 @@ import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@ -91,6 +93,7 @@ import org.springframework.util.xml.StaxUtils; @@ -91,6 +93,7 @@ import org.springframework.util.xml.StaxUtils;
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @author Tadaya Tsuyukubo
* @author Eddú Meléndez
* @since 4.1.1
* @see #build()
* @see #configure(ObjectMapper)
@ -108,6 +111,8 @@ public class Jackson2ObjectMapperBuilder { @@ -108,6 +111,8 @@ public class Jackson2ObjectMapperBuilder {
private final Map<Class<?>, JsonDeserializer<?>> deserializers = new LinkedHashMap<>();
private final Map<PropertyAccessor, JsonAutoDetect.Visibility> visibilities = new HashMap<>();
private final Map<Object, Boolean> features = new HashMap<>();
private boolean createXmlMapper = false;
@ -447,6 +452,18 @@ public class Jackson2ObjectMapperBuilder { @@ -447,6 +452,18 @@ public class Jackson2ObjectMapperBuilder {
return this;
}
/**
* Specify visibility to limit what kind of properties are auto-detected.
* @since 5.1
* @see com.fasterxml.jackson.annotation.PropertyAccessor
* @see com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
*/
public Jackson2ObjectMapperBuilder visibility(PropertyAccessor accessor,
JsonAutoDetect.Visibility visibility) {
this.visibilities.put(accessor, visibility);
return this;
}
/**
* Specify features to enable.
* @see com.fasterxml.jackson.core.JsonParser.Feature
@ -669,6 +686,8 @@ public class Jackson2ObjectMapperBuilder { @@ -669,6 +686,8 @@ public class Jackson2ObjectMapperBuilder {
objectMapper.registerModule(module);
}
this.visibilities.forEach(objectMapper::setVisibility);
customizeDefaultFeatures(objectMapper);
this.features.forEach((feature, enabled) -> configureFeature(objectMapper, feature, enabled));

30
spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,8 +32,10 @@ import java.util.Map; @@ -32,8 +32,10 @@ import java.util.Map;
import java.util.Optional;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -87,6 +89,7 @@ import static org.junit.Assert.assertTrue; @@ -87,6 +89,7 @@ import static org.junit.Assert.assertTrue;
* Test class for {@link Jackson2ObjectMapperBuilder}.
*
* @author Sebastien Deleuze
* @author Eddú Meléndez
*/
@SuppressWarnings("deprecation")
public class Jackson2ObjectMapperBuilderTests {
@ -499,6 +502,19 @@ public class Jackson2ObjectMapperBuilderTests { @@ -499,6 +502,19 @@ public class Jackson2ObjectMapperBuilderTests {
}
@Test
public void visibility() throws JsonProcessingException {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
.visibility(PropertyAccessor.GETTER, Visibility.NONE)
.visibility(PropertyAccessor.FIELD, Visibility.ANY)
.build();
String json = objectMapper.writeValueAsString(new JacksonVisibilityBean());
assertThat(json, containsString("property1"));
assertThat(json, containsString("property2"));
assertThat(json, not(containsString("property3")));
}
public static class CustomIntegerModule extends Module {
@Override
@ -585,4 +601,16 @@ public class Jackson2ObjectMapperBuilderTests { @@ -585,4 +601,16 @@ public class Jackson2ObjectMapperBuilderTests {
}
}
public static class JacksonVisibilityBean {
private String property1;
public String property2;
public String getProperty3() {
return null;
}
}
}

Loading…
Cancel
Save