Table of Contents
Control json serialization/deserialization
1. Usage of @JsonIgnoreProperties
2. @JsonProperty annotation
3. @JsonCreator annotation
4. @JsonSetter and @JsonGetter annotations
5. @JsonAnySetter annotation
6. The @JsonAnyGetter annotation
7. @JsonFormat annotation
8.@JsonSerialize and @JsonDeserialize annotation
SpringBoot serialization rules
1. Global settings
2. Local settings
3. Customize local serialization
4. Customization Global null conversion serialization
Home Java javaTutorial How to solve the serialization and deserialization problems of Json in SpringBoot

How to solve the serialization and deserialization problems of Json in SpringBoot

May 12, 2023 pm 04:07 PM
json springboot

Control json serialization/deserialization

1. Usage of @JsonIgnoreProperties

@JsonIgnoreProperties(value = { "prop1","prop2" })
Copy after login

is used to modify the Pojo class and ignore the specified properties during serialization and deserialization. One or more attributes can be ignored.

@JsonIgnoreProperties(ignoreUnknown = true)
Copy after login

is used to modify the Pojo class and ignore those attributes that cannot be set during deserialization, including those that cannot be set in the constructor and have no corresponding setter methods.

2. @JsonProperty annotation

If the name of the json field is inconsistent with the property name of the Pojo, you can use @JsonProperty to annotate the getter() or setter() method. This annotation sets the json corresponding In addition, @JsonProperty is often used to annotate the formal parameters of the constructor. At this time, the constructor should be annotated with @JsonCreator.

3. @JsonCreator annotation

If the Pojo class definition has Parameter constructor, but if no parameterless constructor is provided, an error will be reported during deserialization. There are two methods:

  • Method 1: Add a parameterless constructor

  • Method 2: Add the @JsonCreator annotation to this parameterized constructor, and the parameters need to be annotated with @JsonProperty.

4. @JsonSetter and @JsonGetter annotations

If the name of the json field is inconsistent with the property name of the Pojo, you can use @JsonGetter to annotate the getter() and @JsonSetter to annotate the setter() method. These two Each annotation can specify a property name. Both annotations can be replaced with @JsonProperty.

5. @JsonAnySetter annotation

Generally, the object property names are determined, such as the Car object , there are named attributes such as brand/price, but sometimes we also need to set some extended attributes for the Car object. The names of these extended attributes are not yet determined. Map are usually used to store the K/ of these extended attributes. V. To deserialize these attributes in the json data into the Map of the class, you need to add a K/V setter method to the class, and this setter method must be annotated with @JsonAnySetter.

public class Car {
    public String brand;
    private Map<String, String> properties;
 
    @JsonAnySetter
    public void add(String key, String value) {
        properties.put(key, value);
    }
}
Copy after login

Once the class is added with @JsonAnySetter, the following json data can be deserialized into the Map:

{
"brand":"Benz",
"attr2":"val2",
"attr1":"val1"
}
Copy after login
Copy after login

6. The @JsonAnyGetter annotation

corresponds to the @JsonAnySetter annotation. If you want to convert the class To serialize the Map K/V attributes into json, you need to add a @JsonAnyGetter method to the class. This method can directly return the KV map.

public class Car {
    public String brand;
    private Map<String, String> properties;
 
    @JsonAnyGetter
    public Map<String, String> getProperties() {
        return properties;
    }
}
Copy after login

The serialized json is:

{
"brand":"Benz",
"attr2":"val2",
"attr1":"val1"
}
Copy after login
Copy after login

7. @JsonFormat annotation

Usually clear the time format used for serialization of Date/Time properties.

public class Event {
    public String name;
 
    @JsonFormat(
      shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
    public Date eventDate;
}
Copy after login

8.@JsonSerialize and @JsonDeserialize annotation

@JsonSerialize annotation You can set a special serialization function for class attributes. The @JsonDeserialize annotation is used to customize the deserialization function for json attributes

SpringBoot serialization rules

  • Include.Include.ALWAYS Default

  • Include.NON_DEFAULT The attribute is the default value and is not serialized

  • Include.NON_EMPTY If the attribute is empty ("") or NULL, it will not be serialized.

  • Include.NON_NULL If the attribute is NULL, it will not be serialized.

1. Global settings

/**
 * 〈返回json空值去掉null和""〉 〈功能详细描述〉
 * 
 * @author gogym
 * @version 2017年10月13日
 * @see JacksonConfig
 * @since
 */
@Configuration
public class JacksonConfig
{
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
    {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        // 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
        // Include.Include.ALWAYS 默认
        // Include.NON_DEFAULT 属性为默认值不序列化
        // Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
        // Include.NON_NULL 属性为NULL 不序列化,就是为null的字段不参加序列化
        //objectMapper.setSerializationInclusion(Include.NON_EMPTY);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        return objectMapper;
    }
}
Copy after login

or

spring.jackson.default-property-inclusion=non_null
spring:
  jackson:
    default-property-inclusion: non_null
Copy after login

2. Local settings

on the entity class or field that needs to be set Add annotation

@JsonInclude(Include.NON_NULL)
Copy after login

3. Customize local serialization

(1), customize a serialization tool class, you need to implement StdSerializer or JsonSerializer

public class ClientObjectSerialize extends JsonSerializer<CreditBorrowerRepaymentRequestDto>{
 @Override
 public void serialize(CreditBorrowerRepaymentRequestDto dto, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
 
  jsonGenerator.writeStartObject();
  try {
   Field[] fields = dto.getClass().getDeclaredFields();
   for (Field field : fields) {
    field.setAccessible(true);
    if(null == field.get(dto)){
     continue; 
    }
    jsonGenerator.writeFieldName(field.getName());
    jsonGenerator.writeObject(field.get(dto));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  jsonGenerator.writeEndObject();
 }
}
Copy after login

(2) Use annotations to act on entity classes

@JsonSerialize(using = ClientObjectSerialize.class)
public class CreditBorrowerRepaymentRequestDto{
}
Copy after login

(3) Can act on entity object fields, process NULL values, or convert

@JsonSerialize(using = ClientStringSerialize.class)
private String name;
 
@JsonSerialize(using = ClientDtaeSerialize.class)
private Date date;
public class ClientStringSerialize extends JsonSerializer<String> {
 
 @Override
 public void serialize(String string, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
 
  if(string == null){
   jsonGenerator.writeString(string + "[NULL]");
  }else{
   jsonGenerator.writeString(string);
  }
 }
}
 
public class ClientDtaeSerialize extends JsonSerializer<Date> {
 @Override
 public void serialize(Date createDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
  jsonGenerator.writeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createDate));
 }
}
Copy after login

4. Customization Global null conversion serialization

SpringBoot returns null value processing in Json data, converts string type null value to "", converts collection array type null value to [], converts original data type null value to 0, convert the Boolean type null value to false, and convert the entity object null value to {}.

(1)Custom null value serialization processor

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
 * 自定义null值序列化处理器
 */
public class CustomizeNullJsonSerializer {
 
 /**
  * 处理数组集合类型的null值
  */
 public static class NullArrayJsonSerializer extends JsonSerializer<Object> {
  @Override
  public void serialize(Object value, JsonGenerator jsonGenerator, 
    SerializerProvider serializerProvider) throws IOException {
   jsonGenerator.writeStartArray();
   jsonGenerator.writeEndArray();
  }
 }
 
 /**
  * 处理字符串类型的null值
  */
 public static class NullStringJsonSerializer extends JsonSerializer<Object> {
  @Override
  public void serialize(Object value, JsonGenerator jsonGenerator, 
    SerializerProvider serializerProvider) throws IOException {
   jsonGenerator.writeString("");
  }
 }
 
 /**
  * 处理数值类型的null值
  */
 public static class NullNumberJsonSerializer extends JsonSerializer<Object> {
  @Override
  public void serialize(Object value, JsonGenerator jsonGenerator, 
        SerializerProvider serializerProvider) throws IOException {
   jsonGenerator.writeNumber(0);
  }
 }
 
 /**
  * 处理boolean类型的null值
  */
 public static class NullBooleanJsonSerializer extends JsonSerializer<Object> {
  @Override
  public void serialize(Object value, JsonGenerator jsonGenerator, 
        SerializerProvider serializerProvider) throws IOException {
   jsonGenerator.writeBoolean(false);
  }
 }
 
 /**
  * 处理实体对象类型的null值
  */
 public static class NullObjectJsonSerializer extends JsonSerializer<Object> {
  @Override
  public void serialize(Object value, JsonGenerator jsonGenerator, 
        SerializerProvider serializerProvider) throws IOException {
   jsonGenerator.writeStartObject();
   jsonGenerator.writeEndObject();
  }
 }
}
Copy after login

(2)Serializer modifier

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import java.util.Collection;
import java.util.List;
 
/**
 * <pre class="brush:php;toolbar:false">
 * 此modifier主要做的事情为:
 * 1.当序列化类型为数组集合时,当值为null时,序列化成[]
 * 2.String类型值序列化为""
 *
 * 
*/ public class MyBeanSerializerModifier extends BeanSerializerModifier { @Override public List changeProperties(SerializationConfig config, BeanDescription beanDesc, List beanProperties) { // 循环所有的beanPropertyWriter for (int i = 0; i < beanProperties.size(); i++) { BeanPropertyWriter writer = beanProperties.get(i); // 判断字段的类型,如果是数组或集合则注册nullSerializer if (isArrayType(writer)) { // 给writer注册一个自己的nullSerializer writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullArrayJsonSerializer()); } if (isStringType(writer)) { writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullStringJsonSerializer()); } } return beanProperties; } /** * 是否是数组 */ private boolean isArrayType(BeanPropertyWriter writer) { Class clazz = writer.getType().getRawClass(); return clazz.isArray() || Collection.class.isAssignableFrom(clazz); } /** * 是否是String */ private boolean isStringType(BeanPropertyWriter writer) { Class clazz = writer.getType().getRawClass(); return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz); } /** * 是否是数值类型 */ private boolean isNumberType(BeanPropertyWriter writer) { Class clazz = writer.getType().getRawClass(); return Number.class.isAssignableFrom(clazz); } /** * 是否是boolean */ private boolean isBooleanType(BeanPropertyWriter writer) { Class clazz = writer.getType().getRawClass(); return clazz.equals(Boolean.class); } }
Copy after login

(3)Configure Jackson entity

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
 
/**
 * 配置Jackson实体
 */
@Configuration
public class JacksonConfig {
 @Bean
 @Primary
 @ConditionalOnMissingBean(ObjectMapper.class)
 public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
  ObjectMapper objectMapper = builder.createXmlMapper(false).build();
  /** 为objectMapper注册一个带有SerializerModifier的Factory */
  objectMapper.setSerializerFactory(objectMapper.getSerializerFactory()
    .withSerializerModifier(new MyBeanSerializerModifier()));
 
  SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
  serializerProvider.setNullValueSerializer(new CustomizeNullJsonSerializer
 .NullObjectJsonSerializer());
  return objectMapper;
 }
}
Copy after login

The above is the detailed content of How to solve the serialization and deserialization problems of Json in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Combination of golang WebSocket and JSON: realizing data transmission and parsing Combination of golang WebSocket and JSON: realizing data transmission and parsing Dec 17, 2023 pm 03:06 PM

The combination of golangWebSocket and JSON: realizing data transmission and parsing In modern Web development, real-time data transmission is becoming more and more important. WebSocket is a protocol used to achieve two-way communication. Unlike the traditional HTTP request-response model, WebSocket allows the server to actively push data to the client. JSON (JavaScriptObjectNotation) is a lightweight format for data exchange that is concise and easy to read.

Comparison and difference analysis between SpringBoot and SpringMVC Comparison and difference analysis between SpringBoot and SpringMVC Dec 29, 2023 am 11:02 AM

SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

What is the difference between MySQL5.7 and MySQL8.0? What is the difference between MySQL5.7 and MySQL8.0? Feb 19, 2024 am 11:21 AM

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Nov 18, 2023 pm 01:59 PM

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string. When writing programs in Golang, we often need to convert the structure into a JSON string. In this process, the json.MarshalIndent function can help us. Implement formatted output. Below we will explain in detail how to use this function and provide specific code examples. First, let's create a structure containing some data. The following is an indication

Pandas usage tutorial: Quick start for reading JSON files Pandas usage tutorial: Quick start for reading JSON files Jan 13, 2024 am 10:15 AM

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

Use PHP's json_encode() function to convert an array or object into a JSON string and format the output Use PHP's json_encode() function to convert an array or object into a JSON string and format the output Nov 03, 2023 pm 03:44 PM

Using PHP's json_encode() function to convert an array or object into a JSON string and format the output can make it easier to transfer and exchange data between different platforms and languages. This article will introduce the basic usage of the json_encode() function and how to format and output a JSON string. 1. Basic usage of json_encode() function The basic syntax of json_encode() function is as follows: stringjson_encod

See all articles