FastJson/spring boot: json输出方法二
2023-09-14 08:57:51 时间
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; } }
相关文章
- 【Spring Boot】 Spring Boot之Hystrix使用介绍
- spring-boot项目在外部tomcat环境下部署
- spring boot: @Entity @Repository一个简单的数据读存储读取
- Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)
- spring boot yaml 配置[三]
- spring boot:解决cors跨域问题的两种方法(spring boot 2.3.2)
- spring boot:使用async异步线程池发送注册邮件(spring boot 2.3.1)
- spring boot集成redis
- 初识spring boot
- [Spring boot] Application properties and configurations
- spring boot @propertySource @importResource @Bean [六]
- intellij idea 2021.2:为一个spring boot项目改名
- spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)
- IntelliJ IDEA创建spring boot项目的方法
- spring boot同时启动多个服务副本(同一服务启动在不同端口)配置方法
- Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析
- 【收藏】Spring Boot项目中使用最新版HBase Java API操作HBase 2.x详解
- 将Spring Boot作为守护进程启动的一种简单的方法
- spring boot报FileSizeLimitExceededException异常的解决方法
- slf4j+logback日志框架 的具体使用操作【spring boot自带的默认日志框架】
- 毕业设计 Spring Boot的疫情防控物业管理系统
- 毕业设计 Spring Boot 房屋出租出售管理系统(含源码+论文)
- java ee wildfly spring 在线程池的线程中注入
- Spring MVC的前端控制器模式
- 001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点
- Spring MVC文本域