赞
踩
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>springbootjackson</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootjackson</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.yl.springbootjackson.model;
import com.fasterxml.jackson.annotation.*;
import java.util.Date;
//序列化/反序列化时批量忽略属性
@JsonIgnoreProperties({"score","message"})
public class User {
@JsonProperty(index = 92)
private String name;
//序列化/反序列化时忽略某一个属性
@JsonIgnore
private Integer sex;
//指定属性序列化/反序列化时的名称,默认就是属性名,value指定新的属性名,index主要用于排序
@JsonProperty(value = "aage",index = 91)
//类似于JsonProperty中的index,用于排序
//@JsonPropertyOrder()
private int age;
@JsonProperty(index = 90)
//日期格式化,注意时区问题
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
private Date birth;
@JsonProperty(index = 89)
private Integer score;
@JsonProperty(index = 88)
private String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", birth=" + birth +
'}';
}
}
package com.yl.springbootjackson.controller;
import com.yl.springbootjackson.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class UserController {
@GetMapping("/getUser")
public User getUser() {
User user = new User();
user.setName("小兰");
user.setAge(19);
user.setBirth(new Date());
return user;
}
@PostMapping("/save")
public void save(@RequestBody User user) {
System.out.println(user);
}
}
package com.yl.springbootjackson.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.text.SimpleDateFormat;
/**
* jackson全局配置方式,注入ObjectMapper即可
*/
@Configuration
public class WebMVCConfig {
@Bean
ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>springbootgson</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootgson</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除jackson依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--gson依赖-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.yl.springbootgson.model;
import com.google.gson.annotations.Expose;
import java.util.Date;
public class User {
@Expose
private String name;
@Expose
private Date birth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
package com.yl.springbootgson.controller;
import com.yl.springbootgson.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class UserController {
@GetMapping("/getUser")
public User getUser() {
User user = new User();
user.setName("小南");
user.setBirth(new Date());
return user;
}
}
spring.gson.date-format=yyyy-MM-dd HH:mm:ss
# 是否禁用HTML的转义字符
spring.gson.disable-html-escaping=true
# 序列化时是否排除内部类
spring.gson.disable-inner-class-serialization=false
# 序列化时是否弃用复杂映射键
spring.gson.enable-complex-map-key-serialization=
# 是否排除没有@Expose注解的字段
spring.gson.exclude-fields-without-expose-annotation=true
# 序列化时字段名的命名策略
spring.gson.field-naming-policy=lower_case_with_underscores
# 在输出之前添加一些特殊的的文本来生成一个不可执行的json
spring.gson.generate-non-executable-json=
# 是否序列化空字段
spring.gson.serialize-nulls=false
package com.yl.springbootgson.config;
import com.google.gson.GsonBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 自定义一个GsonBuilder,配置一些gson的属性
*/
@Configuration
public class WebMVCConfig {
@Bean
GsonBuilder gsonBuilder() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
return gsonBuilder;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>fastjson</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fastjson</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!--fastjson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.yl.fastjson.model;
import java.util.Date;
public class User {
private String name;
private Date birth;
private Integer sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
package com.yl.fastjson.controller;
import com.yl.fastjson.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class UserController {
@GetMapping("/getUser")
public User user() {
User user = new User();
user.setName("小白");
user.setBirth(new Date());
user.setSex(0);
return user;
}
}
package com.yl.fastjson.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
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;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* fastjson如果想要使用,必须提供一个Converter
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
//方式一:继承WebMvcConfigurer,重写configureMessageConverters,将converter添加到converters中
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
List supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
supportedMediaTypes.add(MediaType.APPLICATION_PDF);
supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XML);
supportedMediaTypes.add(MediaType.IMAGE_GIF);
supportedMediaTypes.add(MediaType.IMAGE_JPEG);
supportedMediaTypes.add(MediaType.IMAGE_PNG);
supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
supportedMediaTypes.add(MediaType.TEXT_PLAIN);
supportedMediaTypes.add(MediaType.TEXT_XML);
converter.setSupportedMediaTypes(supportedMediaTypes);
FastJsonConfig config = converter.getFastJsonConfig();
config.setCharset(Charset.forName("UTF-8"));
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(converter);
}
//方式二:注册一个Bean
// @Bean
// FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
// FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
// List supportedMediaTypes = new ArrayList<>();
// supportedMediaTypes.add(MediaType.APPLICATION_JSON);
// supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
// supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
// supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
// supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
// supportedMediaTypes.add(MediaType.APPLICATION_PDF);
// supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
// supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
// supportedMediaTypes.add(MediaType.APPLICATION_XML);
// supportedMediaTypes.add(MediaType.IMAGE_GIF);
// supportedMediaTypes.add(MediaType.IMAGE_JPEG);
// supportedMediaTypes.add(MediaType.IMAGE_PNG);
// supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
// supportedMediaTypes.add(MediaType.TEXT_HTML);
// supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
// supportedMediaTypes.add(MediaType.TEXT_PLAIN);
// supportedMediaTypes.add(MediaType.TEXT_XML);
// converter.setSupportedMediaTypes(supportedMediaTypes);
// FastJsonConfig config = converter.getFastJsonConfig();
// config.setCharset(Charset.forName("UTF-8"));
// config.setDateFormat("yyyy-MM-dd HH:mm:ss");
// config.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName);
// converter.setFastJsonConfig(config);
// converter.setDefaultCharset(Charset.forName("UTF-8"));
// return converter;
// }
}
3.1springboot默认是使用jackson的,对jackson和gson都做了自动配置,所以一般使用这两者之一即可。如果非得要fastjson,那么必须将FastJsonHttpMessageConverter注册到容器中或者实现WebMvcConfigurer接口重写configureMessageConverters方法,将FastJsonHttpMessageConverter对象加到converters集合中即可。
3.2 除此之外,我们也可以自定义jackson或者gson的转换器,然后在里面设置一些自己需要的属性,然后注册到容器中即可使用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。