This commit is contained in:
wing328 2016-10-11 00:48:08 +08:00
commit ba3f2eafd5
39 changed files with 358 additions and 59 deletions

View File

@ -142,6 +142,10 @@ public class JavaClientCodegen extends AbstractJavaCodegen {
} else { } else {
LOGGER.error("Unknown library option (-l/--library): " + getLibrary()); LOGGER.error("Unknown library option (-l/--library): " + getLibrary());
} }
if (additionalProperties.containsKey("jackson") ) {
supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache", invokerFolder, "RFC3339DateFormat.java"));
}
} }
private boolean usesAnyRetrofitLibrary() { private boolean usesAnyRetrofitLibrary() {

View File

@ -126,6 +126,8 @@ public class SpringCodegen extends AbstractJavaCodegen {
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java")); (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java"));
supportingFiles.add(new SupportingFile("swagger2SpringBoot.mustache", supportingFiles.add(new SupportingFile("swagger2SpringBoot.mustache",
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator), "Swagger2SpringBoot.java")); (sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator), "Swagger2SpringBoot.java"));
supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache",
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator), "RFC3339DateFormat.java"));
supportingFiles.add(new SupportingFile("application.mustache", supportingFiles.add(new SupportingFile("application.mustache",
("src.main.resources").replace(".", java.io.File.separator), "application.properties")); ("src.main.resources").replace(".", java.io.File.separator), "application.properties"));
} }
@ -136,6 +138,8 @@ public class SpringCodegen extends AbstractJavaCodegen {
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "WebMvcConfiguration.java")); (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "WebMvcConfiguration.java"));
supportingFiles.add(new SupportingFile("swaggerUiConfiguration.mustache", supportingFiles.add(new SupportingFile("swaggerUiConfiguration.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerUiConfiguration.java")); (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerUiConfiguration.java"));
supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "RFC3339DateFormat.java"));
supportingFiles.add(new SupportingFile("application.properties", supportingFiles.add(new SupportingFile("application.properties",
("src.main.resources").replace(".", java.io.File.separator), "swagger.properties")); ("src.main.resources").replace(".", java.io.File.separator), "swagger.properties"));
} }

View File

@ -40,7 +40,6 @@ import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
@ -96,12 +95,7 @@ public class ApiClient {
} }
public static DateFormat buildDefaultDateFormat() { public static DateFormat buildDefaultDateFormat() {
// Use RFC3339 format for date and datetime. return new RFC3339DateFormat();
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone.
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
} }
/** /**

View File

@ -0,0 +1,21 @@
{{>licenseInfo}}
package {{invokerPackage}};
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -136,6 +136,7 @@ public class ApiClient {
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setDateFormat(new RFC3339DateFormat());
{{^java8}} {{^java8}}
objectMapper.registerModule(new JodaModule()); objectMapper.registerModule(new JodaModule());
{{/java8}} {{/java8}}

View File

@ -8,7 +8,6 @@ import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import feign.codec.EncodeException; import feign.codec.EncodeException;
import feign.codec.Encoder; import feign.codec.Encoder;
@ -32,12 +31,8 @@ public class FormAwareEncoder implements Encoder {
public FormAwareEncoder(Encoder delegate) { public FormAwareEncoder(Encoder delegate) {
this.delegate = delegate; this.delegate = delegate;
// Use RFC3339 format for date and datetime. this.dateFormat = new RFC3339DateFormat();;
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try { try {
this.lineFeedBytes = LINE_FEED.getBytes(UTF_8); this.lineFeedBytes = LINE_FEED.getBytes(UTF_8);
this.boundaryBytes = BOUNDARY.getBytes(UTF_8); this.boundaryBytes = BOUNDARY.getBytes(UTF_8);

View File

@ -39,7 +39,6 @@ import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -70,14 +69,7 @@ public class ApiClient {
json = new JSON(); json = new JSON();
httpClient = buildHttpClient(debugging); httpClient = buildHttpClient(debugging);
// Use RFC3339 format for date and datetime. this.dateFormat = new RFC3339DateFormat();
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.json.setDateFormat((DateFormat) dateFormat.clone());
// Set default User-Agent. // Set default User-Agent.
setUserAgent("{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{artifactVersion}}}/java{{/httpUserAgent}}"); setUserAgent("{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{artifactVersion}}}/java{{/httpUserAgent}}");

View File

@ -24,6 +24,7 @@ public class JSON implements ContextResolver<ObjectMapper> {
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
mapper.setDateFormat(new RFC3339DateFormat());
{{#java8}} {{#java8}}
mapper.registerModule(new JavaTimeModule()); mapper.registerModule(new JavaTimeModule());
{{/java8}} {{/java8}}

View File

@ -1,3 +1,5 @@
springfox.documentation.swagger.v2.path=/api-docs springfox.documentation.swagger.v2.path=/api-docs
server.contextPath={{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}} server.contextPath={{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}}
server.port={{serverPort}} server.port={{serverPort}}
spring.jackson.date-format={{basePackage}}.RFC3339DateFormat
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false

View File

@ -0,0 +1,20 @@
package {{basePackage}};
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -0,0 +1,20 @@
package {{configPackage}};
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -1,14 +1,21 @@
package {{configPackage}}; package {{configPackage}};
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
{{>generatedAnnotation}} {{>generatedAnnotation}}
@Configuration @Configuration
@ComponentScan(basePackages = "{{apiPackage}}") @ComponentScan(basePackages = "{{apiPackage}}")
@ -51,4 +58,14 @@ public class SwaggerUiConfiguration extends WebMvcConfigurerAdapter {
} }
} }
@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));
super.configureMessageConverters(converters);
}
} }

View File

@ -132,6 +132,7 @@ public class ApiClient {
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setDateFormat(new RFC3339DateFormat());
objectMapper.registerModule(new JodaModule()); objectMapper.registerModule(new JodaModule());
return objectMapper; return objectMapper;
} }

View File

@ -8,7 +8,6 @@ import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import feign.codec.EncodeException; import feign.codec.EncodeException;
import feign.codec.Encoder; import feign.codec.Encoder;
@ -32,12 +31,8 @@ public class FormAwareEncoder implements Encoder {
public FormAwareEncoder(Encoder delegate) { public FormAwareEncoder(Encoder delegate) {
this.delegate = delegate; this.delegate = delegate;
// Use RFC3339 format for date and datetime. this.dateFormat = new RFC3339DateFormat();;
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try { try {
this.lineFeedBytes = LINE_FEED.getBytes(UTF_8); this.lineFeedBytes = LINE_FEED.getBytes(UTF_8);
this.boundaryBytes = BOUNDARY.getBytes(UTF_8); this.boundaryBytes = BOUNDARY.getBytes(UTF_8);

View File

@ -0,0 +1,44 @@
/**
* Swagger Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.swagger.client;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -58,7 +58,6 @@ import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import io.swagger.client.auth.Authentication; import io.swagger.client.auth.Authentication;
import io.swagger.client.auth.HttpBasicAuth; import io.swagger.client.auth.HttpBasicAuth;
@ -109,12 +108,7 @@ public class ApiClient {
} }
public static DateFormat buildDefaultDateFormat() { public static DateFormat buildDefaultDateFormat() {
// Use RFC3339 format for date and datetime. return new RFC3339DateFormat();
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone.
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
} }
/** /**

View File

@ -0,0 +1,44 @@
/**
* Swagger Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.swagger.client;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -34,8 +34,8 @@ import io.swagger.client.Pair;
import io.swagger.client.model.Client; import io.swagger.client.model.Client;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.DateTime;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.joda.time.DateTime;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -33,8 +33,8 @@ import io.swagger.client.model.*;
import io.swagger.client.Pair; import io.swagger.client.Pair;
import io.swagger.client.model.Pet; import io.swagger.client.model.Pet;
import java.io.File;
import io.swagger.client.model.ModelApiResponse; import io.swagger.client.model.ModelApiResponse;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -39,7 +39,6 @@ import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -70,14 +69,7 @@ public class ApiClient {
json = new JSON(); json = new JSON();
httpClient = buildHttpClient(debugging); httpClient = buildHttpClient(debugging);
// Use RFC3339 format for date and datetime. this.dateFormat = new RFC3339DateFormat();
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
// Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.json.setDateFormat((DateFormat) dateFormat.clone());
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Swagger-Codegen/1.0.0/java"); setUserAgent("Swagger-Codegen/1.0.0/java");

View File

@ -19,6 +19,7 @@ public class JSON implements ContextResolver<ObjectMapper> {
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
mapper.setDateFormat(new RFC3339DateFormat());
mapper.registerModule(new JodaModule()); mapper.registerModule(new JodaModule());
} }

View File

@ -0,0 +1,44 @@
/**
* Swagger Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.swagger.client;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -1,8 +1,11 @@
package io.swagger.client; package io.swagger.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.std.SqlDateSerializer;
import io.swagger.client.model.Order; import io.swagger.client.model.Order;
import java.lang.Exception; import java.lang.Exception;
import java.sql.Date;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
@ -17,7 +20,7 @@ public class JSONTest {
@Before @Before
public void setup() { public void setup() {
json = new JSON(); json = new ApiClient().getJSON();
order = new Order(); order = new Order();
} }
@ -42,4 +45,18 @@ public class JSONTest {
Order o = json.getContext(null).readValue(str, Order.class); Order o = json.getContext(null).readValue(str, Order.class);
assertEquals(dateStr, dateFormat.print(o.getShipDate())); assertEquals(dateStr, dateFormat.print(o.getShipDate()));
} }
@Test
public void testSqlDateSerialization() throws Exception {
String str = json.getContext(null).writeValueAsString(new java.sql.Date(10));
assertEquals("\"1970-01-01\"", str);
}
@Test
public void testSqlDateDeserialization() throws Exception {
final String str = "1970-01-01";
java.sql.Date date = json.getContext(null).readValue("\"" + str + "\"", java.sql.Date.class);
assertEquals(date.toString(), str);
}
} }

View File

@ -1,8 +1,8 @@
package io.swagger.api; package io.swagger.api;
import io.swagger.model.Client; import io.swagger.model.Client;
import java.time.LocalDate;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.LocalDate;
import java.math.BigDecimal; import java.math.BigDecimal;
import io.swagger.annotations.*; import io.swagger.annotations.*;

View File

@ -1,8 +1,8 @@
package io.swagger.api; package io.swagger.api;
import io.swagger.model.Pet; import io.swagger.model.Pet;
import java.io.File;
import io.swagger.model.ModelApiResponse; import io.swagger.model.ModelApiResponse;
import java.io.File;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;

View File

@ -0,0 +1,20 @@
package io.swagger.configuration;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -1,14 +1,21 @@
package io.swagger.configuration; package io.swagger.configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
@Configuration @Configuration
@ComponentScan(basePackages = "io.swagger.api") @ComponentScan(basePackages = "io.swagger.api")
@ -51,4 +58,14 @@ public class SwaggerUiConfiguration extends WebMvcConfigurerAdapter {
} }
} }
@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));
super.configureMessageConverters(converters);
}
} }

View File

@ -2,8 +2,8 @@ package io.swagger.api;
import io.swagger.model.Client; import io.swagger.model.Client;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.DateTime;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.joda.time.DateTime;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;

View File

@ -2,8 +2,8 @@ package io.swagger.api;
import io.swagger.model.Client; import io.swagger.model.Client;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.DateTime;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.joda.time.DateTime;
import io.swagger.annotations.*; import io.swagger.annotations.*;

View File

@ -1,8 +1,8 @@
package io.swagger.api; package io.swagger.api;
import io.swagger.model.Pet; import io.swagger.model.Pet;
import java.io.File;
import io.swagger.model.ModelApiResponse; import io.swagger.model.ModelApiResponse;
import java.io.File;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;

View File

@ -1,8 +1,8 @@
package io.swagger.api; package io.swagger.api;
import io.swagger.model.Pet; import io.swagger.model.Pet;
import java.io.File;
import io.swagger.model.ModelApiResponse; import io.swagger.model.ModelApiResponse;
import java.io.File;
import io.swagger.annotations.*; import io.swagger.annotations.*;

View File

@ -0,0 +1,20 @@
package io.swagger.configuration;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -1,14 +1,21 @@
package io.swagger.configuration; package io.swagger.configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
@Configuration @Configuration
@ComponentScan(basePackages = "io.swagger.api") @ComponentScan(basePackages = "io.swagger.api")
@ -51,4 +58,14 @@ public class SwaggerUiConfiguration extends WebMvcConfigurerAdapter {
} }
} }
@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));
super.configureMessageConverters(converters);
}
} }

View File

@ -0,0 +1,20 @@
package io.swagger;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import java.text.FieldPosition;
import java.util.Date;
public class RFC3339DateFormat extends ISO8601DateFormat {
// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}
}

View File

@ -2,8 +2,8 @@ package io.swagger.api;
import io.swagger.model.Client; import io.swagger.model.Client;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.DateTime;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.joda.time.DateTime;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;

View File

@ -2,8 +2,8 @@ package io.swagger.api;
import io.swagger.model.Client; import io.swagger.model.Client;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.DateTime;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.joda.time.DateTime;
import io.swagger.annotations.*; import io.swagger.annotations.*;

View File

@ -1,8 +1,8 @@
package io.swagger.api; package io.swagger.api;
import io.swagger.model.Pet; import io.swagger.model.Pet;
import java.io.File;
import io.swagger.model.ModelApiResponse; import io.swagger.model.ModelApiResponse;
import java.io.File;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;

View File

@ -1,8 +1,8 @@
package io.swagger.api; package io.swagger.api;
import io.swagger.model.Pet; import io.swagger.model.Pet;
import java.io.File;
import io.swagger.model.ModelApiResponse; import io.swagger.model.ModelApiResponse;
import java.io.File;
import io.swagger.annotations.*; import io.swagger.annotations.*;

View File

@ -1,3 +1,5 @@
springfox.documentation.swagger.v2.path=/api-docs springfox.documentation.swagger.v2.path=/api-docs
server.contextPath=/v2 server.contextPath=/v2
server.port=8080 server.port=8080
spring.jackson.date-format=io.swagger.RFC3339DateFormat
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false