Reputation: 4164
This is actually a follow up for this question Recreate DTO class without field property instead of having it null using Gson/Jackson and Spring Boot. I originally posted it trying to make it work with Gson but only able to do it with Jackson using the @JsonInclude(JsonInclude.Include.NON_NULL)
but haven't been able to find an equivalent for this with Gson so I can keep that as the library for the project.
Tried using @Expose(serialise=false, deserialise=false) where I had the @JsonInclude annotation or set that field to null as thought Gson by default would ignore that, but it doesn't seem to do it.
Finally, I tried to remove the @Expose annotation completely from to see if Gson would ignore that but not working either.
Pasting it here the main pieces for the issue as well as keeping the extra details added to the original post.
@Service
public class CategoryQueryServiceImpl implements CategoryQueryService {
@Autowired
private CategoryRepository categoryRepository;
@Autowired
private ReportRepository reportRepository;
ObjectMapper mapper = new ObjectMapper();
@Override
public CategoryQueryDto getCategory(UUID id) throws JsonProcessingException {
if (categoryRepository.findById(id).isPresent()) {
Category category = categoryRepository.findById(id).get();
CategoryQueryDto categoryQueryDto = new CategoryQueryDto(category.getId(), category.getTitle());
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String converter = gson.toJson(categoryQueryDto);
categoryQueryDto = gson.fromJson(converter, CategoryQueryDto.class);
// Jackson
//String converter = mapper.writeValueAsString(categoryQueryDto);
//categoryQueryDto = mapper.readValue(converter, CategoryQueryDto.class);
return categoryQueryDto;
} else {
return null;
}
}
@AllArgsConstructor
@NoArgsConstructor
@Data
public class CategoryQueryDto {
@Expose()
private UUID id;
@Expose()
private String title;
// Jackson
// @JsonInclude(JsonInclude.Include.NON_NULL)
private List<ReportQueryDto> reports = null;
public CategoryQueryDto(UUID id, String title) {
this.id = id;
this.title = title;
}
}
If anyone has any other ideas on how to do this please. Thank you very much.
Upvotes: 0
Views: 2328
Reputation: 287
DO NOT serialize null fields (This is the default behavior of Gson serialization)
Employee employeeObj = new Employee(1, "John", "Smith", null);
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
System.out.println(gson.toJson(employeeObj));
Output:
{
"id": 1,
"firstName": "John",
"lastName": "Smith"
}
Serialize null fields (Custom Gson serialization with null values included in JSON output)
Employee employeeObj = new Employee(1, "John", "Smith", null);
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
.create();
System.out.println(gson.toJson(employeeObj));
Output:
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"emailId": null
}
Upvotes: 1
Reputation: 4164
It seems the default behaviour for Gson was not taking place as Spring Boot uses Jackson as the default serialisation library. Overwriting this by pasting the below line within the application.properties
file has fixed the issue for me.
spring.mvc.converters.preferred-json-mapper=gson
Upvotes: 0