zoukankan      html  css  js  c++  java
  • Spring Boot fastJSON的使用

    springBoot,默认使用的json解析框架是Jackson。

    虽然jackson能够满足json的解析,如果想使用熟悉的alibaba的fastjon,我们只需要在pom文件中配置maven依赖就好。
    <!-- fastjson依赖库-->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
    </dependency>
    版本可根据自己需要查询,网址:http://mvnrepository.com/
    Maven依赖完成之后,我们可以通过两种方式配置fastjon
    方法一:启动类继承extends WebMvcConfigurerAdapter,且覆盖configureMessageConverters。
    方法二:在启动类中,注入Bean:HttpMessageConverters。

    代码如下:
    package org.lzm.springbootnew;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

    import java.util.ArrayList;
    import java.util.List;

    @EnableScheduling //定时任务
    @SpringBootApplication
    public class SpringbootNewApplication extends WebMvcConfigurerAdapter {

    1. public static void main(String[] args) {
    2. SpringApplication.run(SpringbootNewApplication.class, args);
    3. }
    4. /*
    5. * // 方法一:extends WebMvcConfigurerAdapter
    6. */
    7. @Override
    8. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    9. super.configureMessageConverters(converters);
    10. //1、先定义一个convert转换消息的对象
    11. FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
    12. //2、添加fastjson的配置信息,比如是否要格式化返回的json数据;
    13. FastJsonConfig fastJsonConfig=new FastJsonConfig();
    14. fastJsonConfig.setSerializerFeatures(
    15. //是否需要格式化
    16. SerializerFeature.PrettyFormat
    17. );
    18. //附加:处理中文乱码(后期添加)
    19. List<MediaType> mediaTypeList=new ArrayList<MediaType>();
    20. mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
    21. fastConverter.setSupportedMediaTypes(mediaTypeList);
    22. //3、在convert中添加配置信息
    23. fastConverter.setFastJsonConfig(fastJsonConfig);
    24. //4、将convert添加到converters
    25. converters.add(fastConverter);
    26. }
    27. /*
    28. * 方法二:在启动类中,注入Bean:HttpMessageConverters
    29. */
    30. @Bean
    31. public HttpMessageConverters fastJsonHttpMessageConverters(){
    32. //1、先定义一个convert转换消息的对象
    33. FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
    34. //2、添加fastjson的配置信息,比如是否要格式化返回的json数据;
    35. FastJsonConfig fastJsonConfig=new FastJsonConfig();
    36. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    37. //附加:处理中文乱码
    38. List<MediaType> fastMedisTypes = new ArrayList<MediaType>();
    39. fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
    40. fastConverter.setSupportedMediaTypes(fastMedisTypes);
    41. //3、在convert中添加配置信息
    42. fastConverter.setFastJsonConfig(fastJsonConfig);
    43. HttpMessageConverter<?> converter=fastConverter;
    44. return new HttpMessageConverters(converter);
    45. }

    }
    其实代码的核心是相同的,这是调取的方式不同而已。两种方式都可以满足我们对于fastjson的依赖使用。

    下面使用@JSONField()注解在实体类中进行验证
    代码如下。
    @JSONField(format = “yyyy-MM-dd”)
    private Date birthday;
    Controller中代码如下。
    @RequestMapping(“/save”)
    public Student save(){
    Student student=new Student();
    student.setName(“婷婷”);
    student.setAge(23);
    student.setSex(“女”);
    student.setBirthday(new Date());
    studentService.save(student);
    return student;
    }
    浏览器返回结果:
    {
    “age”:23,
    “birthday”:”2018-07-10”,
    “id”:97,
    “name”:”婷婷”,
    “sex”:”女”
    }
    除此之外,我们还可以通过@JSONField(serialize = false)来决定字段的显示与否。设置如下。
    @JSONField(serialize = false)
    private Date birthday;
    如果这样设置浏览器返回结果如下,birthday将不再显示。
    {
    “age”:23,
    “id”:97,
    “name”:”婷婷”,
    “sex”:”女”
    }

     
  • 相关阅读:
    MySQL修改配置,区分大小写
    mvc中validateinput属性在asp.net4中不工作
    VS2010开发环境最佳字体及配色
    推荐19个很有用的 JavaScript 库
    Mysql limit 优化,百万至千万级快速分页,复合索引的引用并应用于轻量级框架
    C:\Windows\system32\MSVCR100.dll 没有被指定在 Windows 上运行,或者它包含错误。请尝试使用原始安装媒体重新安装程序,或联系您的系统管理员或软件供应商以获取支持。【解决办法】
    log4net配置步骤
    TraceSource记录程序日志
    [转] WPF TextBox控件中文字实现垂直居中
    SQL Server实现类似split功能
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/10288907.html
Copyright © 2011-2022 走看看