springboot第一章02--用fastjson解析json数据

第一步:添加fastjson依赖

<!--添加 fastjson依赖,必须要1.2.10版本以上的 -->
	<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>fastjson</artifactId>
	    <version>1.2.60</version>
	</dependency>

第二步:配置fastjson有两种方法

第一种方法:

(1)启动类继承:extends WebMvcConfigurerAdapter

(2)覆盖方法:configureMessageConverters

package com.demo;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fasterxml.jackson.databind.SerializationFeature;

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

在user类中添加一个createtime的字段

package com.demo;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class User {
	private int id;
	private String name;
	@JSONField(format = "yyyy-MM-dd HH:mm")
	private Date createTime;//创建时间
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	
	
	
	
}
/**
	 * spring boot默认使用的是jackson,解析出来的是json格式
	 * @return
	 */
	@RequestMapping("/getUser")
	public User getUser(){
		User user = new User();
		user.setId(1);
		user.setName("蜡笔小新");
		user.setCreateTime(new Date());
		return user;
	}

运行测试即可:

名字乱码就不改了,看效果就可以了,解决办法看下面

第二种方法:

(1)在App.java启动类中 注入Bean:HttpMessageConverters

package com.demo;

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

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 用@SpringBootApplication指定这是springboot的启动程序
 * @author fengjinzhu
 *
 */
@SpringBootApplication
public class App {
	
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters(){
		//1.需要先定义一个convert转换消息的对象
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		
		//2.添加fastjson的配置信息,比如:是否要格式化返回的json数据
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		
		//2.1处理中文乱码问题  
        List<MediaType> fastMediaTypes = new ArrayList<>();  
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);  
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
					
		//3.在convert中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
		
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}

	
    public static void main( String[] args ) {
        SpringApplication.run(App.class, args);
    }
}

其余同上,效果同上,不过推荐使用第二种方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值