I use openapi-generator-maven-plugin to generate API models in my spring-boot application
pom.xml:
<...>
<dependencies>
<...>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.2</version>
</dependency>
<...>
</dependencies>
<...>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.0.1</version>
<executions>
<execution>
<id>CLIENT-EVENT</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/swagger/client_event.json
</inputSpec>
<generatorName>spring</generatorName>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<generateApis>false</generateApis>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<generateModels>true</generateModels>
<generateAliasAsModel>false</generateAliasAsModel>
<modelPackage>ru.sb.compliance.lcm.dto.client.event</modelPackage>
<modelsToGenerate>CreateClientEventRequestData,CreateClientEventResponseData</modelsToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
<dateLibrary>java8</dateLibrary>
</configOptions>
<library>spring-boot</library>
<typeMappings>
<typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<...>
The generated LocalDate/LocalDateTime fields in the model look like that:
public class CreateClientEventRequestData {
@JsonProperty("factDt")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime factDt;
<...>
public CreateClientEventRequestData factDt(LocalDateTime factDt) {
this.factDt = factDt;
return this;
}
@Valid
@Schema(name = "factDt", description = "дата и время мероприятия", required = false)
public LocalDateTime getFactDt() {
return factDt;
}
public void setFactDt(LocalDateTime factDt) {
this.factDt = factDt;
}
<...>
}
When I use this model in API, factDt
is serialized like an array:
"data": {
<...>,
"factDt":[2022,11,11,20,18,34,71000000],
<...>
}
What I want to get is
public class CreateClientEventRequestData {
@JsonProperty("factDt")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy HH:mm:ss")
private LocalDateTime factDt;
<...>
}
so that factDt
is serialized like a string:
"data": {
<...>,
"factDt": "11.11.2022 20:18:34",
<...>
}
There is no way to set the default serialization pattern in spring-boot if no @JsonFormat annotation is set
I tried several SO suggestions but neither of them worked, here is what I tried:
- comments to LocalDate is serialized as an array
- https://stackoverflow.com/a/53409514/8990391
- LocalDateTime is representing in array format
- https://stackoverflow.com/a/63698586/8990391 (tried removing @EnableWebMVC from all the configs that had it)
Setting date format doesn't work even with a minimal example like that:
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.setTimeZone(TimeZone.getDefault());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"));
System.out.println(
// prints LocalDateTime in default ISO format and not in "dd.MM.yyyy HH:mm:ss" format...
objectMapper.writeValueAsString(LocalDateTime.now())
);
}
All that makes the idea of generating models from swagger instead of implementing them in your code pretty much deficient...
Your help is greatly appreciated