在上一篇文章中,已经配置好log4j2和druid连接池
上文直通车
https://blog.csdn.net/heheyixiao233/article/details/85321972
现在配置fastjson。maven导入依赖我就不说了,上文中有。
网上的都差不多,一般有两种方法,一种是继承WebMvcConfigurerAdapter重写方法,另一种是@Bean注入第三方的json解析框架。
详见
https://blog.csdn.net/cjq2013/article/details/76421101
https://www.cnblogs.com/soul-wonder/p/9052422.html
我是通过第二种,但网上大多数都是在启动类中写@Bean,我觉的启动类最好不要再添加其他的,再写一个类似FastJsonConfig类不好吗,果然想法是成立的,但要注意名字不能叫FastJsonConfig,因为在fastjson中已经有FastJsonConfig类,如果直接这样起名,就要把jar包地址写上,谁也不想这么麻烦吧
而且还碰上了转换乱码的问题,不过网上的解决方案也没问题,很快就能解决,我就直接贴结果了
在刚才DruidConfig类所在的文件夹,再建一个FastJsonConverterConfig类,写法如下
package com.wanmait.tmallm.config; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @Configuration public class FastJsonConverterConfig { /* 这个方法看似设置了utf-8编码,但仍然会出现乱码 @Bean public HttpMessageConverters fastHttpMessageConverters() { // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonConfig.setCharset(Charset.forName("UTF8")); // 3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); }*/ @Bean public HttpMessageConverters fastJsonHttpMessageConverters(){ //创建FastJson信息转换对象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //创建Fastjosn对象并设定序列化规则 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 中文乱码解决方案 List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//设定json格式且编码为UTF-8 fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); //规则赋予转换对象 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); } }
接下来,通过mybatis****生成好entity、mapper(接口)、mapping(放xml),启动项添加注解如***意添加的注释,后两个注释明白意思,但并不是完全懂,但不写肯定启动不起来)
package com.wanmait.tmallm; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @MapperScan("com.wanmait.tmallm.mapper") @ServletComponentScan public class TmallmApplication { public static void main(String[] args) { SpringApplication.run(TmallmApplication.class, args); } }
运行一下
这是我用fastjson注解格式化的时间
这是我最后得到的结果,时间已经被格式化,我之所以用fastjson就是为了格式化时间 其实jackson也有类似的注释,完全没必要这么麻烦,但我就是想用fastjson,可能它faster吧…