SpringBoot配置HttpMessageConverters

SpringBoot配置HttpMessageConverters

自定义MessageConverters

package com.haixiangpuhui.credit.web.warehouse.consumer.config.msgconvert;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;

/**
 * 配置阿里巴巴 httpMessageConverters
 * @author HX-011
 *
 */
@Configuration
public class FastJsonHttpMessageConvertersConfig extends WebMvcConfigurerAdapter {

	@Bean
	public FastJsonConfig fastJsonConfig() {
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		SerializerFeature writeMapNullValue = SerializerFeature.WriteMapNullValue;
		SerializerFeature WriteNullStringAsEmpty = SerializerFeature.WriteNullStringAsEmpty;
		SerializerFeature WriteNullNumberAsZero = SerializerFeature.WriteNullNumberAsZero;
		SerializerFeature WriteNullListAsEmpty = SerializerFeature.WriteNullListAsEmpty;
		fastJsonConfig.setSerializerFeatures(writeMapNullValue, WriteNullStringAsEmpty, 
				WriteNullNumberAsZero, WriteNullListAsEmpty);
		return fastJsonConfig;
	}

	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters(
			@Qualifier("fastJsonConfig") FastJsonConfig fastJsonConfig) {
		FastJsonHttpMessageConverter4 fastConverter = new FastJsonHttpMessageConverter4();
		fastConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}
}

### 解决方案 为了确保 `LocalDateTime` 在 JSON 响应中作为标准日期时间字符串而不是时间戳返回,在 Spring Boot 中可以通过多种方式来实现这一目标。 #### 方法一:使用 Jackson 注解自定义序列化格式 当实体类中的字段为 `LocalDateTime` 类型时,可以在字段上添加 `@JsonFormat` 注解指定期望的日期时间格式: ```java import com.fasterxml.jackson.annotation.JsonFormat; import java.time.LocalDateTime; public class Event { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime eventTime; // getter and setter methods... } ``` 此方法适用于特定字段定制化的场景[^1]。 #### 方法二:全局配置Jackson ObjectMapper 如果希望整个应用程序统一处理所有的 `LocalDateTime` 字段,则可在应用启动时通过配置文件或编码的方式调整默认的 `ObjectMapper` 行为。具体做法是在项目的配置类中注册一个新的 `HttpMessageConverters` 实例,并设置其使用的 `ObjectMapper` 的序列化特性: ```java import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; @Configuration public class WebConfig { @Bean public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { return builder.modules(new JavaTimeModule()) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build(); } } ``` 上述代码片段禁用了将日期写入为时间戳的功能(`WRITE_DATES_AS_TIMESTAMPS`)并启用了对Java 8 时间API的支持模块 (`JavaTimeModule`). 这样做之后,所有被转换成JSON的对象里的 `LocalDateTime`, `ZonedDateTime` 和其他新的时间类型都会按照ISO-8601的标准格式输出[^2]. #### 方法三:利用 application.properties 或者 yml 文件进行简单配置 最简便的方法之一就是在 `application.properties` (或 `.yml`) 配置文件里加入如下配置项: ```properties spring.jackson.serialization.write_dates_as_timestamps=false ``` 这同样达到了关闭时间戳模式的效果,使得 `LocalDateTime` 对象能够以更易读的形式呈现给客户端[^3].
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值