1

I have a springboot application which is hitting raw api's of the datasource. Now suppose I have a Customer entity with approx 50 fields and I have a raw api for it in which I pass names of the columns and I get the values for that column. Now I am implementing api in springboot which consumes raw api.

I need to implement different api's in springboot for different combinations of the fields of Customer entity and return only those fields setted in object for which user had queried and remove the null valued fields from the object. One way is to implement different dto's for different combinations of the columns of Customer entity. Is there any other way to implement the same in which I don't need to define different dto's for different combinations of the columns of Customer entity in Spring boot ???

Vikas
  • 13
  • 1
  • 4
  • This could be useful for you: https://stackoverflow.com/questions/12707165/spring-rest-service-how-to-configure-to-remove-null-objects-in-json-response – Cassandra S. Jan 04 '21 at 14:45
  • If you annotate your DTO class with `@JsonInclude(JsonInclude.Include.NON_NULL)`, null fields will not show up in JSON. – Roar S. Jan 04 '21 at 15:32
  • Take a look no this solution: [setting-default-values-to-null-fields-when-mapping-with-jackson](https://stackoverflow.com/questions/18805455/setting-default-values-to-null-fields-when-mapping-with-jackson) – Ezequiel Jan 04 '21 at 15:36

3 Answers3

0

with Jackson 2.0 serialization you can specify data inclusion on non nulls at different levels, i.e. on the object mapper (with constructor options), the DTO class or DTO class fields (with annotations). See Jackson annotations here

Emile
  • 141
  • 1
  • 5
0

You can configure the ObjectMapper directly, or make use of the @JsonInclude annotation:

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

OR

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Customer {

    private Long id;
    private String name;
    private String email;
    private String password;

    public Customer() {
    }

    // getter/setter ..
}

You can see how to do it with this sample code:

Customer customer = new Customer();
customer.setId(1L);
customer.setName("Vikas");
customer.setEmail("info@vikas.com");

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String valueAsString = objectMapper.writeValueAsString(customer);

Since the password is left null, you will have an object that does not exist password.

{
  "id": 1,
  "name": "Vikas",
  "email": "info@vikas.com"
}
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
  • I need to return the same string that you had returned in variable valueAsString. But I need to return it as a object and not as string. – Vikas Jan 05 '21 at 11:48
-1

This can be done using @JsonInclude inside the DTO class. Please refer following code block for ignoring null values.

@JsonInclude(Include.NON_NULL) // ignoring null values 
@Data //lombock
@Builder //builder pattern 
public class Customer {

    private Long id;
    private String name;
    private String email;
    private String password;
}