Add auto-configuration for Jackson's parameter names module

The parameter names module allows users of Java 8 that have compiled
their code with the -parameters option to avoid the name for
annotations to map the json onto constructor and method parameters
with the names of the parameters being used instead.

This commit adds auto-configuration for the module that will only
be enabled when running on Java 8.

Closes gh-3804
pull/3942/merge
Andy Wilkinson 9 years ago
parent 6c59e75874
commit 7f2be6ce27

@ -55,6 +55,10 @@
<artifactId>jackson-datatype-jsr310</artifactId> <artifactId>jackson-datatype-jsr310</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>

@ -33,6 +33,8 @@ import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -44,12 +46,14 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat; import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer; import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
/** /**
* Auto configuration for Jackson. The following auto-configuration will get applied: * Auto configuration for Jackson. The following auto-configuration will get applied:
@ -129,6 +133,19 @@ public class JacksonAutoConfiguration {
} }
@Configuration
@ConditionalOnJava(JavaVersion.EIGHT)
@ConditionalOnClass(ParameterNamesModule.class)
static class ParameterNamesModuleConfiguration {
@Bean
@ConditionalOnMissingBean(ParameterNamesModule.class)
public ParameterNamesModule parametersNameModule() {
return new ParameterNamesModule(JsonCreator.Mode.PROPERTIES);
}
}
@Configuration @Configuration
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class }) @ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
@EnableConfigurationProperties(JacksonProperties.class) @EnableConfigurationProperties(JacksonProperties.class)

@ -37,6 +37,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
@ -50,8 +52,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy; import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.StdDateFormat; import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
@ -412,6 +416,28 @@ public class JacksonAutoConfigurationTests {
objectMapper.writeValueAsString(dateTime)); objectMapper.writeValueAsString(dateTime));
} }
@Test
public void parameterNamesModuleIsAutoConfigured() {
assertParameterNamesModuleCreatorBinding(Mode.PROPERTIES,
JacksonAutoConfiguration.class);
}
@Test
public void customParameterNamesModuleCanBeConfigured() {
assertParameterNamesModuleCreatorBinding(Mode.DELEGATING,
ParameterNamesModuleConfig.class, JacksonAutoConfiguration.class);
}
private void assertParameterNamesModuleCreatorBinding(Mode expectedMode,
Class<?>... configClasses) {
this.context.register(configClasses);
this.context.refresh();
Annotated annotated = mock(Annotated.class);
Mode mode = this.context.getBean(ObjectMapper.class).getDeserializationConfig()
.getAnnotationIntrospector().findCreatorBinding(annotated);
assertThat(mode, is(equalTo(expectedMode)));
}
public static class MyDateFormat extends SimpleDateFormat { public static class MyDateFormat extends SimpleDateFormat {
public MyDateFormat() { public MyDateFormat() {
@ -469,6 +495,16 @@ public class JacksonAutoConfigurationTests {
} }
@Configuration
protected static class ParameterNamesModuleConfig {
@Bean
public ParameterNamesModule parameterNamesModule() {
return new ParameterNamesModule(JsonCreator.Mode.DELEGATING);
}
}
protected static final class Foo { protected static final class Foo {
private String name; private String name;

Loading…
Cancel
Save