Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker 诊断工具


Spring Boot使用 Jackson 注解

Spring Boot Jackson JSON 大约 1199 字

示例

Bean

@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
//@JsonPropertyOrder(value = {"date", "a-username"})
@JsonPropertyOrder(alphabetic = true)
public class Test {

    @JsonIgnore
    private String id;

    @JsonProperty("a-username")
    private String name;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="Asia/Shanghai")
    private Date date;

    private String nil;

}

Controller

@RestController
public class TestController {

    @GetMapping("/")
    public Test test() {
        Test test = new Test();
        test.setId("this is id");
        test.setName("this is name");
        test.setDate(new Date());
        return test;
    }

}

输出

{"a-username":"this is name","date":"2021-01-05 11:11:49"}

注解含义

@JsonIgnore

序列化或反序列化时忽略指定字段。

@JsonIgnore

@JsonProperty

序列化或反序列化时更改字段名称。

@JsonProperty("a-username")

@JsonFormat

序列化或反序列化时格式化日期类。

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="Asia/Shanghai")

@JsonInclude

序列化或反序列化时忽略为null""的字段。

@JsonInclude(JsonInclude.Include.NON_EMPTY)

@JsonPropertyOrder

序列化或反序列化时指定字段顺序。

按字母表顺序。

@JsonPropertyOrder(alphabetic = true)

按指定顺序。

@JsonPropertyOrder(value = {"date", "a-username"})
阅读 1172 · 发布于 2021-01-07

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批