Spring Boot 3 OAS3 API返回Epoch而非DateTime格式问题求助
Spring Boot 3(OAS3)API返回Epoch时间而非期望的时间戳格式
我通过openapi-generator-maven-plugin生成Spring Boot 3(OAS3)API,当前遇到问题:接口响应中的DateTime字段,期望返回时间戳格式,但实际输出的是纳秒级Epoch值(如1675435210.336232)。部分自定义Jackson日期映射器可正常工作,但控制器中对象的默认序列化无效。
已尝试以下方案但未解决:
- 在
application.yaml中配置日期序列化参数 - 在SpringBootApplication类中定义自定义
@Bean覆盖日期映射规则 - 在实体类上直接设置自定义日期格式
API契约(api.yaml)字段定义:
sentDate: type: string format: date-time
生成的Java实体代码:
@JsonProperty("sentDate") @DateTimeFormat( iso = ISO.DATE_TIME ) private OffsetDateTime sentDate;
实际响应示例:
"sentDate": 1675435210.336232,
解决方案
1. 全局Jackson配置修正
Spring Boot 3默认使用Jackson 2.15+,对于OffsetDateTime,若要输出毫秒级时间戳,需在application.yaml中添加以下配置(覆盖生成器默认的纳秒级序列化规则):
spring: jackson: serialization: write-dates-as-timestamps: true deserialization: read-date-timestamps-as-nanoseconds: false
read-date-timestamps-as-nanoseconds: false会强制Jackson输出毫秒级时间戳,而非当前的纳秒级Epoch值。
2. 自定义Jackson模块强制序列化规则
若全局配置不生效,可能是生成代码使用了独立的ObjectMapper。创建自定义Jackson模块,指定OffsetDateTime的序列化方式:
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.ser.OffsetDateTimeSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; @Configuration public class JacksonConfig { @Bean public SimpleModule offsetDateTimeTimestampModule() { SimpleModule module = new SimpleModule(); // 序列化OffsetDateTime为毫秒级时间戳 module.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer(DateTimeFormatter.ISO_INSTANT)); return module; } }
3. 修改openapi-generator插件配置
在Maven插件配置中添加参数,让生成的实体类自动绑定自定义序列化器:
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>6.6.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec> <generatorName>spring</generatorName> <configOptions> <dateLibrary>java8</dateLibrary> <jackson>true</jackson> <serializationLibrary>jackson</serializationLibrary> <!-- 为生成的模型类添加序列化注解 --> <additionalModelTypeAnnotations>@com.fasterxml.jackson.databind.annotation.JsonSerialize(using = com.yourpackage.OffsetDateTimeTimestampSerializer.class)</additionalModelTypeAnnotations> </configOptions> </configuration> </execution> </executions> </plugin>
同时实现自定义序列化器:
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import java.io.IOException; import java.time.OffsetDateTime; public class OffsetDateTimeTimestampSerializer extends StdSerializer<OffsetDateTime> { protected OffsetDateTimeTimestampSerializer() { super(OffsetDateTime.class); } @Override public void serialize(OffsetDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException { // 输出毫秒级时间戳 gen.writeNumber(value.toInstant().toEpochMilli()); } }
4. 检查生成代码中的ObjectMapper配置
部分情况下,openapi-generator会生成独立的ObjectMapper配置类(如OpenAPIDocumentationConfig),需修改该类添加时间戳序列化配置:
@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); // 启用时间戳序列化,禁用纳秒解析 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); return mapper; }
内容的提问来源于stack exchange,提问作者JustM
相关产品推荐
相关产品推荐

