Spring Boot使用 Jackson 注解

Spring Boot Jackson JSON About 1,199 words

示例

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"})
Views: 1,559 · Posted: 2021-01-07

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

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

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


Today On History
Browsing Refresh