0

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.

aeroboy
  • 149
  • 4
  • 14
  • 1
    Setting `spring.jackson.serialization.write-dates-as-timestamps = false` in the application properties may help. Usually this makes spring (i.e. jackson) to serialize the date as an ISO date string. – Martin Hauner May 31 '20 at 06:35

2 Answers2

1

I did it the same thing in ObjectMapper object, while passing it to resttemplate:

objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
aeroboy
  • 149
  • 4
  • 14
1

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;
    }

}
Martin
  • 598
  • 1
  • 4
  • 27