Issue 5413 - Use ConstructorBased Injection for ObjectMapper (#5430)

* Converting to constructor based injection

* Correcting spelling mistake

* Adding the bean for ObjectMapper

* Adding files modified by running the petstore scripts

* Adding final qualifier to objectMapper variable in apiController mustache
This commit is contained in:
Elizabeth Thomas
2017-04-25 10:11:18 -05:00
committed by wing328
parent 745951c1ce
commit efb337dd92
9 changed files with 72 additions and 40 deletions

View File

@@ -32,6 +32,12 @@ import javax.validation.Valid;
@Controller
{{#operations}}
public class {{classname}}Controller implements {{classname}} {
private final ObjectMapper objectMapper;
public {{classname}}Controller(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
{{#isDelegate}}
private final {{classname}}Delegate delegate;
@@ -49,11 +55,7 @@ public class {{classname}}Controller implements {{classname}} {
{{^isDelegate}}
{{^async}}
{{#examples}}
{{#-first}}
ObjectMapper objectMapper = new ObjectMapper();
{{/-first}}
if (accept != null && accept.contains("{{{contentType}}}")) {
return new ResponseEntity<{{>returnTypes}}>(objectMapper.readValue("{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}", {{>exampleReturnTypes}}.class), HttpStatus.OK);
}

View File

@@ -6,6 +6,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
@@ -58,14 +59,23 @@ public class SwaggerUiConfiguration extends WebMvcConfigurerAdapter {
}
}
@Bean
public Jackson2ObjectMapperBuilder builder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.indentOutput(true)
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.dateFormat(new RFC3339DateFormat());
return builder;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.dateFormat( new RFC3339DateFormat())
.build();
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
super.configureMessageConverters(converters);
}
@Bean
public ObjectMapper objectMapper(){
return builder().build();
}
}