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);
}
}

springboot使用阿里fastjson来解析数据的更多相关文章

  1. SpringBoot 03_利用FastJson返回Json数据

    自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...

  2. Android商城开发系列(六)——使用 OkHttpUtils 请求网络 + 使用 fastjson解析数据

    OkHttp是Google推荐使用的一个开源的网络请求框架,Android开发中涉及到网络请求和接口调用现在大部分都是使用OkHttp,网上已经有不少人针对OkHttp进行了封装,这里推荐一下鸿洋大神 ...

  3. 阿里fastjson解析

    解析案例 String object="{total=1, rows=[{_Account=3646808, UserID=131514, Mt4Name=SewwoaIQQS, Serve ...

  4. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

  5. SpringBoot整合阿里云OSS文件上传、下载、查看、删除

    1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...

  6. 记录心得-FastJson分层解析demo示例

    记录一下,平时用到,可速查!关键: // startArray(); 开始解析数组 // endArray(); 结束解析数组 // startObject(); 开始解析键值对 // endObje ...

  7. Java操作JSON数据(3)--fastjson操作JSON数据

    fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean.本文介绍下fastjs ...

  8. SpringBoot整合阿里短信服务

    导读 由于最近手头上需要做个Message Gateway,涉及到:邮件(点我直达).短信.公众号(点我直达)等推送功能,网上学习下,整理下来以备以后使用. 步骤 点我直达 登录短信服务控制台 点我直 ...

  9. 通过python将阿里云DNS解析作为DDNS使用

    通过python将阿里云DNS解析作为DDNS使用 脚本需要Python2.x运行 安装alidns python sdk sudo pip install aliyun-python-sdk-ali ...

随机推荐

  1. 付哇刷脸支付系统源码V1.03完整安装包.zip

    付哇刷脸支付系统源码是什么? 1.是一款专业的刷脸+聚合支付平台源码系统: 2.支持对接自己的支付宝和微信官方服务商: 3.基于目前流行的WEB2.0的架构(php+mysql),采用自研DOXCX框 ...

  2. Python面向对象 | 抽象类和接口类

    一.抽象类(规范的编程模式) 什么是抽象类 抽象类是一个特殊的类,它的特殊之处在于只能被继承,不能被实例化.抽象类的本质还是类,指的是一组类的相似性,而接口只强调函数属性的相似性. 为什么要有抽象类 ...

  3. day 19

    If you think you can, you can. And if you think you can't, you're right.

  4. PATB1021个数统计

    参考代码: #include<cstdio> #include<cstring> #include<cstdlib> int main() { char str[1 ...

  5. SOA & 微服务

    参考文档: https://www.cnblogs.com/renzhitian/p/6853289.htmlhttp://www.jdon.com/soa.htmlhttps://www.ibm.c ...

  6. libevent笔记1:安装及DEMO

    本篇简单记录了libevent的安装过程及基础的先进先出管道Demo,其中demo来自这篇博客,安装过程在这篇博客 实验环境 系统:Ubuntu 18.04.3 libevent版本:libevent ...

  7. Intellij 热部署插件 JRebel [转载]

    原文:https://blog.csdn.net/weixin_42831477/article/details/82229436 Intellij热部署插件JRebel IDEA本身没有集成热部署工 ...

  8. 【Activiti学习之四】Activiti API(三)

    环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.启动流程 多种方式启动 package com.wjy.pro; import or ...

  9. ASP.NET Core WebApi基于Redis实现Token接口安全认证

    一.课程介绍 明人不说暗话,跟着阿笨一起玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将会是需要思考的问题.在ASP.NET WebSer ...

  10. rent a apartment

    今日焦点 month-to-month 按月 6-month minimum 至少六个月 sublease 转租 drenched in sunlight 阳光充足的 词汇实践 I am lookin ...