Reputation: 159
Hi I am using swagger code gen (v3) to generate the service client in my Spring Boot project using the following configuration in my pom.xml
<configuration>
<language>java</language>
<inputSpec>specs.yaml</inputSpec>
<apiPackage>api</apiPackage>
<modelPackage>model</modelPackage>
<invokerPackage>invoker</invokerPackage>
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateSupportingFiles>true</generateSupportingFiles>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<typeMappings>
<typeMapping>DateTime=LocalDateTime</typeMapping>
<typeMapping>Date=LocalDate</typeMapping>
</typeMappings>
<importMappings>
<importMapping>LocalDateTime=java.time.OffsetDateTime</importMapping>
<importMapping>LocalDate=java.time.LocalDate</importMapping>
</importMappings>
<configOptions>
<library>resttemplate</library>
<interfaceOnly>true</interfaceOnly>
<useTags>true</useTags>
<useBeanValidation>true</useBeanValidation>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<sourceFolder>/<sourceFolder>
</configOptions>
</configuration>
Issue I am facing is while performing the POST request through the Api Client generated through above configuration. POST request looks like
{
"name": "abc",
"type": "xyz",
"businessdata": {
"currency": "INR",
"startDate": [2020,5,28],
"endDate": [2021,12,25],
"code": "X123"
},
"seqnumber": "987"
}
Here startDate and endDate are LocalDate in Java class and those are also generated by Open Api specs. Here I am not understanding why it is converting to brackets [] and hence other system which is receiving this request is throwing invalid date exception.
Please let me know how to fix this issue and explain in detail as I am new to swagger code gen.
Upvotes: 0
Views: 3461
Reputation: 716
A complete solution to update ObjectMapper
in all RestTemplate
instances:
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate getRestClient() {
RestTemplate template = new RestTemplate();
template.getMessageConverters().stream()
.filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter)
.map(converter -> (AbstractJackson2HttpMessageConverter) converter)
.forEach(converter -> converter.getObjectMapper()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
);
return template;
}
}
Upvotes: 1
Reputation: 159
I did it the same thing in ObjectMapper
object, while passing it to resttemplate
:
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
Upvotes: 1