@JsonAutoDetect详解

背景就是某银行影像系统,上传下载字段全是大写的,可以用该注解解决。

@JsonAutoDetect 定义字段、方法的访问可见性规则

默认情况下,Jackson 只使用 public 的字段进行序列化和反序列化。没有 public 字段时,会使用 public 的 getters/setters。可以通过 @JsonAutoDetect 自定义这种行为,指定字段、方法的可见性规则。

源码

@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonAutoDetect
{
    /**
     * Enumeration for possible visibility thresholds (minimum visibility)
     * that can be used to limit which methods (and fields) are
     * auto-detected.
     */
    public enum Visibility {
        ANY, 					// 所有的访问修饰符
        NON_PRIVATE, 			// 除 private 之外的
        PROTECTED_AND_PUBLIC, 	// 允许 protected、public
        PUBLIC_ONLY, 			// 只允许 public
        NONE, 					// 禁用 AutoDetect
        DEFAULT;
    }
    
    Visibility getterVisibility() default Visibility.DEFAULT;	// for getXyz()

    Visibility isGetterVisibility() default Visibility.DEFAULT;	// for isXyz()
    
    Visibility setterVisibility() default Visibility.DEFAULT;	// for setXyz()

    Visibility creatorVisibility() default Visibility.DEFAULT;	// for constructor or factory methods

    Visibility fieldVisibility() default Visibility.DEFAULT;	// for fields
}        

举个栗子

package com.lwq.test;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

@JsonAutoDetect(
        fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Employee {
    private String name;
    private String dept;
    private String address;

    public static Employee of(String name, String dept, String address) {
        Employee e = new Employee();
        e.name = name;
        e.dept = dept;
        e.address = address;
        return e;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", dept='" + dept + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
public class ExampleMain {
    public static void main(String[] args) throws IOException {
        Employee employee = Employee.of("Trish", "Admin", "421 Moon Hill");
        ObjectMapper om = new ObjectMapper();
        String jsonString = om.writeValueAsString(employee);
        System.out.println(jsonString);
        Employee e = om.readValue(jsonString, Employee.class);
        System.out.println(e);
    }
}

运行结果

image

不使用 @JsonAutoDetect

删除 @JsonAutoDetect 将抛出异常,因为 Employee 中没有可用的字段或 getter/setters:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.lwq.test.Employee and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1277)
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
	at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)
	at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4409)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3663)
	at com.lwq.test.ExampleMain.main(ExampleMain.java:11)

参考博客
https://blog.csdn.net/qq_20919883/article/details/116424408

posted @ 2022-05-05 11:44  卡卡罗特琪琪  阅读(2392)  评论(0编辑  收藏  举报