mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-07 19:08:52 +00:00
[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:
parent
2680995825
commit
6f2c139ff8
@ -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"));
|
||||
}
|
||||
|
@ -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
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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.*;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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.*;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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.*;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -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.*;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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.*;
|
||||
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user