Spring Boot中使用FastJson解析Json数据

首先我们创建一个maven工程,如下图:
Spring Boot中使用FastJson解析Json数据

第二步:配置pom.xml

<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>
    <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.7 ,默认是1.6 -->
    <java.version>1.7</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>

    <!-- spring boot devtools 依赖包. -->
    <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
           <optional>true</optional>
          <scope>true</scope>
    </dependency>

  </dependencies>

第三步:创建如下图所示的包结构和类

Spring Boot中使用FastJson解析Json数据
第四步:编写上面三个类
User:
Spring Boot中使用FastJson解析Json数据
提供get、set方法。

FastJsonController:

Spring Boot中使用FastJson解析Json数据

App:

Spring Boot中使用FastJson解析Json数据

上述步骤完成后,执行App类的main方法,等待项目启动完毕。然后在浏览器中访问
http://localhost:8080/getJson,返回结果如下图
Spring Boot中使用FastJson解析Json数据
发现中文乱码,这时修改controller类,如下图:
Spring Boot中使用FastJson解析Json数据

然后再次访问,结果如下图:
Spring Boot中使用FastJson解析Json数据
解决乱码问题。

猜你喜欢

转载自blog.51cto.com/13587708/2137033