方式一,添加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); } }View Code
方式二,继承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); } }View Code
问题记录:
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) {}
标签:fastjson,FastJsonHttpMessageConverter,JAVA,Springboot,fastConverter,springframew From: https://www.cnblogs.com/leonlipfsj/p/16849606.html