失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > FastJson/spring boot: json输出

FastJson/spring boot: json输出

时间:2020-12-12 23:54:23

相关推荐

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="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /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></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.App.java继承WebMvcConfigurerAdapter,然后复写configureMessageConverters方法,加入自定义的FastJson配置

/*** 复写configureMessageConverters*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// TODO Auto-generated method stub//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);//3、在convert中添加配置信息.fastConverter.setFastJsonConfig(fastJsonConfig);//4、将convert添加到converters当中.converters.add(fastConverter);}

APP.JAVA参考

package com.muyang.boot1;import java.util.List;import org.apache.log4j.Logger;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.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;/*** Hello world!**/@SpringBootApplicationpublic class App extends WebMvcConfigurerAdapter{private static Logger logger = Logger.getLogger(App.class);/*** 复写configureMessageConverters*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// TODO Auto-generated method stub//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);//3、在convert中添加配置信息.fastConverter.setFastJsonConfig(fastJsonConfig);//4、将convert添加到converters当中.converters.add(fastConverter);}public static void main( String[] args ){//System.out.println( "Hello World!" );\SpringApplication.run(App.class, args);logger.info("--sprint-boot-");}}

3.建立Demo.java类,用来创建json输出之前的对象,并创建new Date()日期格式化属性

JSONField(format="yyyy-MM-dd HH:mm")

private Date createTime

/*** 格式化时间*/@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;}}

4.HelloController.java控制器

普通输出

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

json输出

/*** charset解决中文乱码**/@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;@RestControllerpublic 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输出》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。