2、需要在App.java中继承WebMvcConfigurerAdapter重写方法:configureMessageConverters 添加我们自己定义的json解析框架;2.1 @Bean注入第三方的json解析框架:@Beanpublic 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);}
/** * Spring Boot默认使用的json解析框架是jackson * @return */ @RequestMapping("/getDemo") public person getDemo(){ person demo = new person(); demo.setAge(1); demo.setName("张三"); demo.setCreateTime(new Date()); demo.setRemarks("这是备注信息"); return demo; }