简体   繁体   中英

How to ignore some json fields in LogBook response?

I need to ignore some fields in response of Zalando Logbook logging

For example, if I have a Java response class:

class MyResponse {
    private String fieldOne;
    private String fieldTwo;
    private String fieldThree;
}

I want to have a LOGGED response json:

{
    "fieldOne": "abcd",
    "fieldThree": "dcba"
}

I've tried to use BodyFilters, but it's still not working. How can I solve it?

It depends what library do you use to serialize your class to JSON. If you use JSON-Jackson library you will need to add @JsonIgnore annotation to the field(s) that you want to ignore. So your class should look like this:

class MyResponse {
    private String fieldOne;
    
    @JsonIgnore
    private String fieldTwo;
    private String fieldThree;
}

Read this article for more info: Jackson Ignore Properties on Marshalling . Also, there is an open source MgntUtils library that provides a JsonUtils class that allows you very simply to serialize classes to JSON String and parse them from JSON String to class instance. This util is a thing wrapper over JSON-Jackson library and, for simple cases, saves you writing any configuration related to the Jackson lib. Here is the Javadoc for JsonUtils class . The MgntUtils library can be obtained as Maven artifact from Maven Central or from Github (including Javadoc and source code). This library is written and maintained by me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM