|
@@ -15,28 +15,44 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
+import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
|
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
import java.time.LocalTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
|
-public class JacksonConfig {
|
|
|
|
|
|
|
+public class JacksonConfig implements WebMvcConfigurer {
|
|
|
|
|
|
|
|
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
private static final DateTimeFormatter DATE_TIME_LENIENT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[:ss]");
|
|
private static final DateTimeFormatter DATE_TIME_LENIENT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[:ss]");
|
|
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 关键:通过 WebMvcConfigurer 直接配置消息转换器
|
|
|
|
|
+ * 确保自定义 ObjectMapper 被 Spring MVC 的 HTTP 响应序列化使用
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
|
|
+ MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
|
|
|
|
+ converter.setObjectMapper(objectMapper());
|
|
|
|
|
+ converters.add(0, converter); // 添加到最前面,优先使用
|
|
|
|
|
+ System.out.println("[Jackson配置] 已通过 WebMvcConfigurer 注入自定义 ObjectMapper");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Bean
|
|
@Bean
|
|
|
@Primary
|
|
@Primary
|
|
|
public ObjectMapper objectMapper() {
|
|
public ObjectMapper objectMapper() {
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
|
|
// 注册Long类型转String序列化模块,避免前端精度丢失
|
|
// 注册Long类型转String序列化模块,避免前端精度丢失
|
|
|
- SimpleModule longModule = new SimpleModule();
|
|
|
|
|
|
|
+ SimpleModule longModule = new SimpleModule("LongToStringModule");
|
|
|
longModule.addSerializer(Long.class, ToStringSerializer.instance);
|
|
longModule.addSerializer(Long.class, ToStringSerializer.instance);
|
|
|
longModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
|
|
longModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
|
|
|
mapper.registerModule(longModule);
|
|
mapper.registerModule(longModule);
|
|
@@ -61,6 +77,7 @@ public class JacksonConfig {
|
|
|
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
|
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
|
|
|
|
|
|
|
|
+ System.out.println("[Jackson配置] ObjectMapper 初始化完成,Long -> String 序列化已启用");
|
|
|
return mapper;
|
|
return mapper;
|
|
|
}
|
|
}
|
|
|
|
|
|