FastJson/spring boot: json输出方法二

Posted 穆晟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastJson/spring boot: json输出方法二相关的知识,希望对你有一定的参考价值。

1.引入FastJson依赖包

 <!-- FastJson -->
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>fastjson</artifactId>
    	<version>1.2.15</version>
    </dependency>

  

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.muyang</groupId>
  <artifactId>boot1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>boot1</name>
  <url>http://maven.apache.org</url>
  
  
  <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <!--<version>2.0.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>
  	<!-- 
  		自动依赖parent里面的版本
  		<version></version>
  	 -->
  </dependency>
  
  <!--  dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency-->
  
  
    <!-- FastJson -->
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>fastjson</artifactId>
    	<version>1.2.15</version>
    </dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    
    
  </dependencies>
  
  
  <build>
  	<finalName>boot1</finalName>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>${java.version}</source>
  				<target>${java.version}</target>  				
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
  
</project>

  

2.Demo.java对象参加

引用FastJson中的额JSONField(format="yyyy-MM-dd HH:mm")格式化new Date()日期

/**
	 * 格式化时间
	 */
	@JSONField(format="yyyy-MM-dd HH:mm")
	private Date createTime;	
	
	
	/**
	 * 不显示json
	 */
	@JSONField(serialize=false)
	private String remarks;

  

demo.java参考

package com.muyang.boot1;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class Demo {

	private int id;
	
	private String name;
	
	/**
	 * 格式化时间
	 */
	@JSONField(format="yyyy-MM-dd HH:mm")
	private Date createTime;	
	
	
	/**
	 * 不显示json
	 */
	@JSONField(serialize=false)
	private String remarks;
	
		
	

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

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	
	
	
}

  

3.创建App2.java启动器,同时配置FastJson配置

/**
	 * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
	 * @return
	 */
	@Bean
	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);
	}

  

App2.java参考

package com.muyang.boot1;

import org.apache.log4j.Logger;
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.converter.HttpMessageConverter;

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

@SpringBootApplication
public class App2 {

	
	private static Logger logger = Logger.getLogger(App2.class);
	
	
	/**
	 * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
	 * @return
	 */
	@Bean
	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)
	{
		SpringApplication.run(App2.class, args);
	}
	
}

  

 

4.创建HelloController.java控制器

普通输出

@RequestMapping(value="/hello")
	public String hello()
	{
		return "hello";
	}

  

json输出

@RequestMapping(value="/getDemo", produces = "application/json; charset=utf-8")
	public Demo getDemo()
	{
		Demo demo = new Demo();
		demo.setId(1);
		demo.setName("张三");
		demo.setCreateTime(new Date());
		demo.setRemarks("这是备注信息");
		return demo;
	}

  

HelloController.java参考

package com.muyang.boot1;

import java.util.Date;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	
	@RequestMapping(value="/hello")
	public String hello()
	{
		return "hello";
	}
	
	
	@RequestMapping(value="/getDemo", produces = "application/json; charset=utf-8")
	public Demo getDemo()
	{
		Demo demo = new Demo();
		demo.setId(1);
		demo.setName("张三");
		demo.setCreateTime(new Date());
		demo.setRemarks("这是备注信息");
		return demo;
	}
	
}

  

 

以上是关于FastJson/spring boot: json输出方法二的主要内容,如果未能解决你的问题,请参考以下文章

spring boot常用的注解

为 Spring Boot 制作黑名单 JWT 令牌

访问PCF Spring Boot应用程序上的VCAP属性

尽管有FileNotFoundException,但是Java Spring Boot配置文件仍被正确处理

spring-boot-framework 如何自动将对象返回成json格式

JSON with C program