Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore JSON field in response alone in java

Tags:

java

json

I want to exclude name and age from my response, but When I receive the JSON payload request I need name and age field - after my business logic, I want to send status and message as part of JSON response. name and age should exclude from that. How can I achieve this in java?

public class Sample {

    private String name;
    private String age;
    private String status;
    private String message;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

My Controller class:

@PostMapping(path = "/testApp", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> test(@RequestBody Sample sample) {
    Sample response = myService.calculate(sample);
    return new ResponseEntity<Object>(response, HttpStatus.OK);
}

My Request

{
    "name": "Mark",
    "age": "48"
}

My Response

{
    "status": "200",
    "message": "success"
}
like image 616
learn groovy Avatar asked Oct 27 '25 11:10

learn groovy


1 Answers

Using Jackson for Java/JSON serialization and deserialization provides a number of ways to control the JSON that is produced and accepted.

Since you want to omit fields in certain cases, the most straightforward way to do this is with the @JsonIgnore annotation.

@JsonIgnore can be used on fields, getters, and setters (and more) to always ignore the field, ignore on output to JSON, or ignore on JSON->Java deserialization.

Your basic Sample class could ignore message all the time (both when serializing and deserializing) by annotating the field:

public class Sample {
    private String name;
    private String age;
    private String status;
    @JsonIgnore private String message;
}

When you want to omit a field when serializing Java -> JSON you can annotate the getter, when you want to ignore a field when deserializing JSON -> Java you annotate the setter.
So if you want to omit name and age when your Sample object is producing JSON, you'd annotate both those fields

public class Sample {

    private String name;
    private String age;
    private String status;
    private String message;

    @JsonIgnore        // Added Annotation
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @JsonIgnore        // Added Annotation
    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

With those annotations, name and age will be set in the object when you deserialize from JSON, but they will not be output when serializing to JSON, and you need only one class, not separate classes for the request and the response.

like image 92
Stephen P Avatar answered Oct 30 '25 01:10

Stephen P