Alibaba </groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> package com.example.demo; import java.util.List; 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.WebMvcConfigurer; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @Configuration public class MyWebmvcConfiguration implements WebMvcConfigurer{ @Override public void extendMessageConverters(List<HttpMessageConverter<? >> converters) { FastJsonHttpMessageConverter fjc = new FastJsonHttpMessageConverter(); FastJsonConfig fj = new FastJsonConfig(); fj.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect); fjc.setFastJsonConfig(fj); converters.add(fjc); } } package com.example.demo; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.util.StreamUtils; import java.io.IOException; import java.nio.charset.Charset; public class MyMessageConverter extends AbstractHttpMessageConverter<UserEntity> { public MyMessageConverter() { // Create a new custom MediaType application/xxx-junlin super(new MediaType("application", "xxx-junlin", charset.forname (" utf-8 ")); } @Override protected boolean supports(Class<? > clazz) {// indicates that only parameters of type UserEntity are processed. return UserEntity.class.isAssignableFrom(clazz); } /** * Override the readlntenal method to process the requested data. The code indicates that we process the data separated by "-" and turn it into an object of type UserEntity. */ @Override protected UserEntity readInternal(Class<? extends UserEntity> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { String temp = StreamUtils.copyToString(inputMessage.getBody(), Charset.forName("UTF-8")); String[] tempArr = temp.split("-"); return new UserEntity(tempArr[0],tempArr[1]); } /** * rewrite writeInternal to handle how to output data to response. */ @Override protected void writeInternal(UserEntity userEntity, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { String out = "hello: " + userEntity.getName() + "-" + userEntity.getAddress(); outputMessage.getBody().write(out.getBytes()); } } package com.example.demo; import java.util.List; 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.WebMvcConfigurer; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @Configuration public class MyWebmvcConfiguration implements WebMvcConfigurer{ @Override public void extendMessageConverters(List<HttpMessageConverter<? >> converters) { FastJsonHttpMessageConverter fjc = new FastJsonHttpMessageConverter(); FastJsonConfig fj = new FastJsonConfig(); fj.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect); fjc.setFastJsonConfig(fj); converters.add(fjc); converters.add(converter()); } @Bean public MyMessageConverter converter() { return new MyMessageConverter(); } } package com.example.demo; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping(value="/get",method=RequestMethod.GET) public Object getList(){ List<UserEntity> list= new ArrayList<UserEntity>(); UserEntity u1 = new UserEntity(null, "shanghai"); list.add(u1); return list; } @RequestMapping(method = RequestMethod.POST, value = "/convert") public @ResponseBody UserEntity converter(@RequestBody UserEntity user) { return user; }}Copy the code

Reference: www.cnblogs.com/hhhshct/p/9…