5

Goal

How can I exclude all empty json objects {}, arrays [] or arrays of empty objects [{},{}] from the JSON response produced by Jackson in a RestController ?

Requirements and context

I'm building a Spring Boot REST API for a client. The API sends requests to a database and must produce a JSON response.

Constraints:

  • The DAO layer sends native SQL queries and receives huge DB results as a List. Developers have to manually map this result to Java objects using indexes (see code below)
  • SQL queries return a lot of null values (these queries can NOT be modified). Because of these null values, Java objects having only null-valued fields are frequently instantiated

Requirements:

  • All fields having null value must be excluded from JSON responses. Already implemented using Jackson @JsonInclude(JsonInclude.Include.NON_NULL) annotation
  • All empty json objects {} or arrays [], or arrays of empty objects [{},{}] must be excluded from the JSON response. This is where I'm stuck (Please see example below)

Code

Manual mapping in DAO layer:

public List<A> daoMethod() {
    List<Object[]> dbResult = getDbResults();
    List<A> javaObjects = new ArrayList<>();

    // build nested Java objects
    for (Object[] line in dbResult) {
        A a = new A();
        a.setProp1(line[0]);
        a.setProp2(line[1]);
        // and so on...
        javaObjects.add(a);
        return javaObjects ;
    }
}

Controller method:

public ResponseEntity<A> controllerMethod() {
    List<A> javaObjects = myDao.daoMethod();
    return new ResponseEntity(javaObjects, HttpStatus.OK);
}

All DTO classes which must be serialized in the JSON response extend the BaseDto class:

@JsonInclude(JsonInclude.Include.NON_NULL) // removes all fields having NULL value
public abstract class BaseDto implements Serializable{
    // some properties...
}

Actual and expected results

Current JSON output:

{
  prop1: "some string",
  prop2: [{},{},{}],
  prop3: [],
  prop4: {},
}

Expected:

{
  prop1: "some string"
}
orange00001
  • 51
  • 1
  • 3
  • please refer the following link **https://stackoverflow.com/questions/16089651/jackson-serialization-ignore-empty-values-or-nullhttps://stackoverflow.com/questions/16089651/jackson-serialization-ignore-empty-values-or-null** – karthick S Jun 21 '19 at 03:50
  • Did you find any solution? I'm having the same problem. I also tried JsonInclude.NON_EMPTY,JsonInclude.NON_NULL but without any luck – Bad_Pan Jan 31 '20 at 21:24
  • Any one got solution for this ?? – Masoom Raza Sep 27 '21 at 10:38

2 Answers2

1

Try to use NON_EMPTY

@JsonInclude(JsonInclude.Include.NON_EMPTY) 

Value that indicates that only properties with null value, or what is considered empty, are not to be included.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • 2
    Thank you. I tried using this annotation with different combinations (class level, type level...) but it doesn't work – orange00001 Jun 20 '19 at 12:34
0

NON_EMPTY can be used in application.properties to exclude empty objects (including null objects) in response.

spring.jackson.default-property-inclusion=NON_EMPTY

By the way NON_NULL is also applicable to remove null objects only in response.