zoukankan      html  css  js  c++  java
  • spring boot json 首字母大小写问题解决方案

       spring boot默认使用的json解析框架是jackson,对于.net转java的项目来说太坑了,首字母大写的属性会自动转为小写,然后前端就悲剧了,十几个属性的ViewModel增加几个JsonField注解能解决问题,但若有几十上百个属性,那就只能换json框架了,幸好有fastjson能原样输出属性,下面是spring boot 使用fastjson的实施步骤,原文来自https://blog.csdn.net/cjq2013/article/details/76421101。

    1.spring boot默认使用的json解析框架是jackson,使用fastjson需要配置,首先引入fastjson依赖 
    pom.xml配置如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>spring.boot.helloworld</groupId>
      <artifactId>helloworld</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>helloworld Maven Webapp</name>
      <url>http://maven.apache.org</url>
    
      <!--
      spring boot父节点依赖,引入这个之后相关的引入就不需要加version配置,spring boot
      会自动选择最合适的版本进行添加
      -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      <!-- fastjson依赖 -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.15</version>
        </dependency>
    </dependencies>
      <!--构建节点-->
      <build>
        <finalName>helloworld</finalName>
    
      </build>
    </project>
    

    2.配置有两种方式,一种是继承WebMvcConfigurerAdapter重写方法,另一种是@Bean注入第三方的json解析框架。 
    (1)继承WebMvcConfigurerAdapter

    /**
     * Created by struggle on 2017/7/29.
     */
    @SpringBootApplication//指定这是一个Spring boot应用程序
    public class App extends WebMvcConfigurerAdapter{
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            super.configureMessageConverters(converters);
            /*
            1.需要先定义一个convert转换消息的对象;
            2.添加fastjson的配置信息,比如是否要格式化返回的json数据
            3.在convert中添加配置信息
            4.将convert添加到converters中
             */
            //1.定义一个convert转换消息对象
            FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
            //2.添加fastjson的配置信息,比如:是否要格式化返回json数据
            FastJsonConfig fastJsonConfig=new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(
                    SerializerFeature.PrettyFormat
            );
            fastConverter.setFastJsonConfig(fastJsonConfig);
            converters.add(fastConverter);
        }
    
        public static void main(String[] args) {
            /**
             * 在main方法中启动应用程序
             */
            SpringApplication.run(App.class,args);
        }
    }

    (2)使用@Bean注入方式

    /**
     * Created by struggle on 2017/7/29.
     */
    @SpringBootApplication//指定这是一个Spring boot应用程序
    public class App{
    
        @Bean//使用@Bean注入fastJsonHttpMessageConvert
        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);
        }
        public static void main(String[] args) {
            /**
             * 在main方法中启动应用程序
             */
            SpringApplication.run(App.class,args);
        }
    }

    3.使用上面一种方式即可,项目目录结构如下 
    这里写图片描述 
    App.java

    /**
     * Created by struggle on 2017/7/29.
     */
    //extends WebMvcConfigurerAdapter
    @SpringBootApplication//指定这是一个Spring boot应用程序
    public class App{
    
    //    @Override
    //    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //        super.configureMessageConverters(converters);
    //        /*
    //        1.需要先定义一个convert转换消息的对象;
    //        2.添加fastjson的配置信息,比如是否要格式化返回的json数据
    //        3.在convert中添加配置信息
    //        4.将convert添加到converters中
    //         */
    //        //1.定义一个convert转换消息对象
    //        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
    //        //2.添加fastjson的配置信息,比如:是否要格式化返回json数据
    //        FastJsonConfig fastJsonConfig=new FastJsonConfig();
    //        fastJsonConfig.setSerializerFeatures(
    //                SerializerFeature.PrettyFormat
    //        );
    //        fastConverter.setFastJsonConfig(fastJsonConfig);
    //        converters.add(fastConverter);
    //    }
        @Bean//使用@Bean注入fastJsonHttpMessageConvert
        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);
        }
        public static void main(String[] args) {
            /**
             * 在main方法中启动应用程序
             */
            SpringApplication.run(App.class,args);
        }
    }

    HelloController.java

    @RestController//等价于@Controller和@ResponseBody
    public class HelloController {
        @RequestMapping("/hello")//建立请求映射
        public String hello(){
            return "hello";
        }
        @RequestMapping("/getUser")
        public User getUser(){
            User user=new User();
            user.setName("张三");
            user.setPass("123");
            user.setCreateTime(new Date());
            user.setRemarks("this is remark");
            return user;
        }
    }
    

    User.java

    public class User {
        String name;
        String pass;
        /*
         如果不想返回remarks
         deserialize是否需要序列化属性
         */
        @JSONField(serialize = false)
        String remarks;
    
        public String getRemarks() {
            return remarks;
        }
    
        public void setRemarks(String remarks) {
            this.remarks = remarks;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        @JSONField(format = "yyyy-MM-dd HH:mm:ss")
        Date createTime;//创建时间
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPass() {
            return pass;
        }
    
        public void setPass(String pass) {
            this.pass = pass;
        }
    }

    4.在User.java中,可以在属性上加注解 @JSONField(format = “yyyy-MM-dd HH:mm:ss”)格式化日期, @JSONField(serialize = false)属性不序列化,以及其他spring-fastjson注解对解析json 
    5.运行App.java类进行测试 
    这里写图片描述

    访问http://localhost:8080/getUser

    这里写图片描述

    6.源码百度云网盘地址链接:http://pan.baidu.com/s/1hsIe1RA 密码:ghlz

  • 相关阅读:
    directX--关于CSource和CSourceStream (谁调用了fillbuffer) 分类: DirectX 2014-11-11 08:10 635人阅读 评论(0) 收藏
    把连续动态bmp转换为avi 分类: 文件格式 VC++ ffmpeg-SDL-VLC-Live555 DirectX 2014-11-07 14:54 516人阅读 评论(0) 收藏
    ioftpd read/write 锁实现
    编译 ioftpd v7.7.3
    3D Math Primer for Graphics and Game Development -- 图形与游戏开发(3D数学基础) (简介)
    E. Turn Off The TV Educational Codeforces Round 29
    D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
    D. Dog Show 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
    D. Huge Strings Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
    E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)
  • 原文地址:https://www.cnblogs.com/yym/p/9008922.html
Copyright © 2011-2022 走看看