0

i have a rest api, i get the data using DAO, mapping it from class to another DTO class, if no data present in database, instead of returning null like this

{
"tes":null,
"tez": null,
"example": null
}

i want to return like this:

{
"tes":"",
"tez": "",
"example": ""
}

i have more than 100 field. so i think its not good to set value in setter getter in all field. how to do this is simple way?

Vodka
  • 113
  • 2
  • 10

2 Answers2

0

As I see you have two options.

  • Instead of manualy mapping from DAO to DTO Class you need to use a ModelMapper Class with

    modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());

  • As your problem is not clearly the mapping but generating JSON from your DTO, you can use Jackson

    @JsonInclude(Include.NON_NULL)

This will skip the empty/null values in your generated JSON.

See: Spring REST Service: how to configure to remove null objects in json response

Tristate
  • 1,498
  • 2
  • 18
  • 38
  • i dont want to exclude null. i want to change every null to " ". i tried use json include, still get null if no data in database. – Vodka Sep 22 '20 at 15:27
0

You could use Lombok like so

@Builder.Default
private String tes = "my-default-value";  
rogger2016
  • 821
  • 3
  • 11
  • 28