5

There's a model class used by multiple APIs wherein if fields in this model class is null it is returned as null in the JSON response. However, another API is using the same model class and here the null fields should be omitted in the response.

Is there a way to achieve this without creating another copy of the model file and just to ingore the null fields with @JsonInclude(Include.NON_NULL)

Example,

class A { private String b; private String c; private String d; // getters and setters } 

Response 1 needed

"A": { "b": "val1", "c": null, "d": "val2" } 

Response 2 needed (omit values that are null)

"A": { "b": "val1", "d": "val2" } 

Is there a way to achieve these behaviors using a single model class? Some sort of config while instantiation or something?

4
  • Then I guess you will have to do something like this: stackoverflow.com/a/35672470/14072498 Commented Jan 18, 2021 at 21:35
  • This looks like adding even more code than creating a copy of this class and annotating it :) Commented Jan 18, 2021 at 21:39
  • Yes, I know, it ain't pretty. Was a guy at the very end of the thread that was talking about modifying the DTO using reflection. Trying to find something more concrete about that. Commented Jan 18, 2021 at 21:40
  • This question is similar to: For Spring Boot 1.2.3, how to set ignore null value in JSON serialization?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 4, 2024 at 15:10

2 Answers 2

3

After reading a lot of doc and code, both from blogs and here on SO, I'm tempted to say that the number of extra codelines and complexity required to achieve what you want, does not justify the effort.

With the example below, you'll get an extra class, but it is minimal. Use class A when you want to include null fields, else use class B.

@Data public class A { private String b; private String c; private String d; } @JsonInclude(JsonInclude.Include.NON_NULL) public class B extends A { } 

When serializing these classes, JSON may look like this for class A

{ "b": "val1", "c": null, "d": "val2" } 

and like this for Class B

{ "b": "val1", "d": "val2" } 

If you are not using Lombok in your project yet, please add this to your POM in order to make @Data work:

 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> 

Furthermore if you keep getting no arguments error you can use @SuperBuilder and @Data annotations on both entities instead

Sign up to request clarification or add additional context in comments.

Comments

2

Try to use Jackson package as follow:

import com.fasterxml.jackson.annotation.JsonInclude; @JsonInclude(JsonInclude.Include.NON_NULL) class A { private String b; private String c; private String d; // getters and setters } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.