Spring Boot 支持三种 Json 库:
- Gson
- Jackson
- JSON-B
默认使用的是Jackson
一、Jackson方式
全局配置类:
package com.junya.thymeleaf;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.text.SimpleDateFormat;
/**
* @author ZHANGCHAO
* @date 2020/5/15 9:39
* @since 1.0.0
*/
@Configuration
public class JsonConfig {
// @Bean
// MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
// MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
// ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
// converter.setObjectMapper(objectMapper);
// return converter;
// }
@Bean
ObjectMapper mapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
return objectMapper;
}
}
或者在实体类中加@JsonFormat注解:
package com.junya.thymeleaf.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* @author ZHANGCHAO
* @date 2020/5/15 8:43
* @since 1.0.0
*/
@Data
public class Book {
private Integer id;
private String name;
private String author;
private Double price;
@JsonFormat(locale = "yyyy-MM-dd hh:mm:ss")
private Date date;
}
二、Gson方式
-
首先需要在pom中屏蔽掉Jackson,引入Gson依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
-
Gson配置类
package com.junya.thymeleaf; import com.google.gson.GsonBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.GsonHttpMessageConverter; /** * @author ZHANGCHAO * @date 2020/5/15 9:39 * @since 1.0.0 */ @Configuration public class JsonConfig { @Bean GsonHttpMessageConverter gsonHttpMessageConverter() { GsonHttpMessageConverter converter = new GsonHttpMessageConverter(); converter.setGson(new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create()); return converter; } }
三、Fastjson方式
-
去掉SpringBoot默认的Jackson,引入Fastjson依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68</version> </dependency>
-
Fastjson配置类
package com.junya.thymeleaf; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author ZHANGCHAO * @date 2020/5/15 9:39 * @since 1.0.0 */ @Configuration public class JsonConfig { @Bean FastJsonHttpMessageConverter fastJsonHttpMessageConverter() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd hh:mm:ss"); converter.setFastJsonConfig(config); return converter; } }
最终效果: