[Spring] Format datetime in rfc3339 (#3777)

* [spring-boot] format datetime in rfc3339

See #3727

* [spring-mvc] format datetime in rfc3339
This commit is contained in:
Christophe Bornet 2016-10-10 18:43:28 +02:00 committed by wing328
parent 2680995825
commit 6f2c139ff8
21 changed files with 171 additions and 12 deletions

View File

@ -126,6 +126,8 @@ public class SpringCodegen extends AbstractJavaCodegen {
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java"));
supportingFiles.add(new SupportingFile("swagger2SpringBoot.mustache",
(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",
("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"));
supportingFiles.add(new SupportingFile("swaggerUiConfiguration.mustache",
(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",
("src.main.resources").replace(".", java.io.File.separator), "swagger.properties"));
}

View File

@ -1,3 +1,5 @@
springfox.documentation.swagger.v2.path=/api-docs
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}};
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
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.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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
{{>generatedAnnotation}}
@Configuration
@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

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

View File

@ -1,8 +1,8 @@
package io.swagger.api;
import io.swagger.model.Pet;
import java.io.File;
import io.swagger.model.ModelApiResponse;
import java.io.File;
import io.swagger.annotations.*;
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;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
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.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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
@Configuration
@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 org.joda.time.LocalDate;
import org.joda.time.DateTime;
import java.math.BigDecimal;
import org.joda.time.DateTime;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;

View File

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

View File

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

View File

@ -1,8 +1,8 @@
package io.swagger.api;
import io.swagger.model.Pet;
import java.io.File;
import io.swagger.model.ModelApiResponse;
import java.io.File;
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;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
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.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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
@Configuration
@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 org.joda.time.LocalDate;
import org.joda.time.DateTime;
import java.math.BigDecimal;
import org.joda.time.DateTime;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;

View File

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

View File

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

View File

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

View File

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