方式一,添加HttpMessageConverters实例

import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 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; /** * json填充到java对象 */ @Configuration public class MyConverter { @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); // fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } }
方式二,继承WebMvcConfigurationSupport

import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; @Configuration public class BackendWebMvcConfig extends WebMvcConfigurationSupport { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //定义一个convert转换消息的对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //添加fastjson的配置信息,比如是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( //是否输出值为null的字段,默认为false SerializerFeature.WriteMapNullValue, //将Collection类型字段的字段空值输出为[] SerializerFeature.WriteNullListAsEmpty, //将字符串类型字段的空值输出为空字符串 SerializerFeature.WriteNullStringAsEmpty); //在convert中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); //设置支持的媒体类型 fastConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON)); //设置默认字符集 fastConverter.setDefaultCharset(StandardCharsets.UTF_8); //将convert添加到converters converters.add(0, fastConverter); //解决返回字符串带双引号问题 StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN)); stringHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8); converters.add(0, stringHttpMessageConverter); super.addDefaultHttpMessageConverters(converters); } }
问题记录:
1、对象校验报错:
/**
* 用户id
*/
@JSONField(name = "user_id")
@NotEmpty()
private Long userId;
报:No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.Long'. Check configuration for 'userId'
NotEmpty只能用于string判空,Long类型不支持,改成@NotNull
说明:
1、restful接口中使用注解@Validate和Valid都可用,Validated支持分组
@PostMapping(value = "edit")
@ResponseBody
public HttpResult edit(@RequestBody @Validate User user) {}
le.li
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了