forked from loafle/openapi-generator-original
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:
parent
745951c1ce
commit
efb337dd92
@ -13,7 +13,6 @@ import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:1.5.+'
|
||||
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
|
||||
classpath 'com.android.tools.build:gradle:2.3.+'
|
||||
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,11 +25,11 @@ if(hasProperty('target') && target == 'android') {
|
||||
apply plugin: 'com.github.dcendents.android-maven'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion '23.0.2'
|
||||
compileSdkVersion 25
|
||||
buildToolsVersion '25.0.2'
|
||||
defaultConfig {
|
||||
minSdkVersion 14
|
||||
targetSdkVersion 23
|
||||
targetSdkVersion 25
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_7
|
||||
|
@ -26,12 +26,16 @@ import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
public class FakeApiController implements FakeApi {
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public FakeApiController(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public ResponseEntity<Client> testClientModel(@ApiParam(value = "client model" ,required=true ) @Valid @RequestBody Client body,
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<Client>(objectMapper.readValue("{ \"client\" : \"aeiou\"}", Client.class), HttpStatus.OK);
|
||||
}
|
||||
|
@ -25,6 +25,12 @@ import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
public class PetApiController implements PetApi {
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public PetApiController(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public ResponseEntity<Void> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @Valid @RequestBody Pet body,
|
||||
@RequestHeader("Accept") String accept) {
|
||||
// do some magic!
|
||||
@ -42,12 +48,11 @@ public class PetApiController implements PetApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/xml")) {
|
||||
return new ResponseEntity<List<Pet>>(objectMapper.readValue("<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status></Pet>", List.class), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<List<Pet>>(objectMapper.readValue("[ { \"photoUrls\" : [ \"aeiou\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"aeiou\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"aeiou\", \"id\" : 1 } ], \"status\" : \"available\"} ]", List.class), HttpStatus.OK);
|
||||
}
|
||||
@ -59,12 +64,11 @@ public class PetApiController implements PetApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/xml")) {
|
||||
return new ResponseEntity<List<Pet>>(objectMapper.readValue("<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status></Pet>", List.class), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<List<Pet>>(objectMapper.readValue("[ { \"photoUrls\" : [ \"aeiou\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"aeiou\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"aeiou\", \"id\" : 1 } ], \"status\" : \"available\"} ]", List.class), HttpStatus.OK);
|
||||
}
|
||||
@ -76,12 +80,11 @@ public class PetApiController implements PetApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/xml")) {
|
||||
return new ResponseEntity<Pet>(objectMapper.readValue("<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status></Pet>", Pet.class), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<Pet>(objectMapper.readValue("{ \"photoUrls\" : [ \"aeiou\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"aeiou\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"aeiou\", \"id\" : 1 } ], \"status\" : \"available\"}", Pet.class), HttpStatus.OK);
|
||||
}
|
||||
@ -109,8 +112,6 @@ public class PetApiController implements PetApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<ModelApiResponse>(objectMapper.readValue("{ \"code\" : 0, \"type\" : \"aeiou\", \"message\" : \"aeiou\"}", ModelApiResponse.class), HttpStatus.OK);
|
||||
}
|
||||
|
@ -24,6 +24,12 @@ import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
public class StoreApiController implements StoreApi {
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public StoreApiController(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public ResponseEntity<Void> deleteOrder(@ApiParam(value = "ID of the order that needs to be deleted",required=true ) @PathVariable("order_id") String orderId,
|
||||
@RequestHeader("Accept") String accept) {
|
||||
// do some magic!
|
||||
@ -33,8 +39,6 @@ public class StoreApiController implements StoreApi {
|
||||
public ResponseEntity<Map<String, Integer>> getInventory(@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<Map<String, Integer>>(objectMapper.readValue("{ \"key\" : 0}", Map.class), HttpStatus.OK);
|
||||
}
|
||||
@ -46,12 +50,11 @@ public class StoreApiController implements StoreApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/xml")) {
|
||||
return new ResponseEntity<Order>(objectMapper.readValue("<Order> <id>123456789</id> <petId>123456789</petId> <quantity>123</quantity> <shipDate>2000-01-23T04:56:07.000Z</shipDate> <status>aeiou</status> <complete>true</complete></Order>", Order.class), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<Order>(objectMapper.readValue("{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\"}", Order.class), HttpStatus.OK);
|
||||
}
|
||||
@ -63,12 +66,11 @@ public class StoreApiController implements StoreApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/xml")) {
|
||||
return new ResponseEntity<Order>(objectMapper.readValue("<Order> <id>123456789</id> <petId>123456789</petId> <quantity>123</quantity> <shipDate>2000-01-23T04:56:07.000Z</shipDate> <status>aeiou</status> <complete>true</complete></Order>", Order.class), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<Order>(objectMapper.readValue("{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\"}", Order.class), HttpStatus.OK);
|
||||
}
|
||||
|
@ -24,6 +24,12 @@ import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
public class UserApiController implements UserApi {
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public UserApiController(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public ResponseEntity<Void> createUser(@ApiParam(value = "Created user object" ,required=true ) @Valid @RequestBody User body,
|
||||
@RequestHeader("Accept") String accept) {
|
||||
// do some magic!
|
||||
@ -52,12 +58,11 @@ public class UserApiController implements UserApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/xml")) {
|
||||
return new ResponseEntity<User>(objectMapper.readValue("<User> <id>123456789</id> <username>aeiou</username> <firstName>aeiou</firstName> <lastName>aeiou</lastName> <email>aeiou</email> <password>aeiou</password> <phone>aeiou</phone> <userStatus>123</userStatus></User>", User.class), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<User>(objectMapper.readValue("{ \"firstName\" : \"aeiou\", \"lastName\" : \"aeiou\", \"password\" : \"aeiou\", \"userStatus\" : 6, \"phone\" : \"aeiou\", \"id\" : 0, \"email\" : \"aeiou\", \"username\" : \"aeiou\"}", User.class), HttpStatus.OK);
|
||||
}
|
||||
@ -70,12 +75,11 @@ public class UserApiController implements UserApi {
|
||||
@RequestHeader("Accept") String accept) throws IOException {
|
||||
// do some magic!
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
if (accept != null && accept.contains("application/xml")) {
|
||||
return new ResponseEntity<String>(objectMapper.readValue("aeiou", String.class), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
if (accept != null && accept.contains("application/json")) {
|
||||
return new ResponseEntity<String>(objectMapper.readValue("\"aeiou\"", String.class), HttpStatus.OK);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user