简体   繁体   中英

Recreate DTO class without field property instead of having it null using Gson/Jackson and Spring Boot

I have a DTO class that returns this:

{
  "id": "fdbfb1ec-1f1e-4867-9cc8-73929fbcc07e",
  "title": "My another category",
  "reports": null
}

When I actually would like for some of my api calls to have the reports key as not part of it as per the below, rather than being set to null.

{
  "id": "fdbfb1ec-1f1e-4867-9cc8-73929fbcc07e",
  "title": "My another category",
}

I tried to use Gson and Expose annotation believing this would remove my key, but it just seems to be turning it to null instead. I tried both using @Expose(serialize = false, deserialize = false) or leaving it without the annotation since my Gson object uses the excludeFieldsWithoutExposeAnnotation() piece but both give me the same outcome. However, I can see my string converter excludes the reports key and gives me this {"id":"fdbfb1ec-1f1e-4867-9cc8-73929fbcc07e","title":"My another category"} but not sure why the property is still there when the object is recreated and if the only solution for this scenario would not be through Gson but have two complete different DTOs instead, one with that property and the other without it?

@AllArgsConstructor
@NoArgsConstructor
@Data
public class CategoryQueryDto {

    @Expose()
    private UUID id;
    @Expose()
    private String title;

    @Expose(serialize = false, deserialize = false)
    private List<ReportQueryDto> reports;

    public CategoryQueryDto(String title) {
        this.title = title;
    }

    public CategoryQueryDto(UUID id, String title) {
        this.id = id;
        this.title = title;
    }

}


@Override
public CategoryQueryDto getCategory(UUID id) {

    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);


        return categoryQueryDto;
    } else {
        return null;
    }

}

Thank you very much.

在此处输入图像描述

UPDATE

Here is my code on Github https://github.com/francislainy/gatling_tool_backend

Tried with Jackson rather than Gson and got the same issue.

Used @JsonInclude(JsonInclude.Include.NON_NULL) on the field I wanted to show only if not null and it worked.

@AllArgsConstructor
@NoArgsConstructor
@Data
public class CategoryQueryDto {

@Expose()
private UUID id;
@Expose()
private String title;

@JsonInclude(JsonInclude.Include.NON_NULL)
private List<ReportQueryDto> reports = null;

public CategoryQueryDto(String title) {
    this.title = title;
}

public CategoryQueryDto(UUID id, String title) {
    this.id = id;
    this.title = title;
}

}

And here my controller with Jackson

@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());

        
        String converter = mapper.writeValueAsString(categoryQueryDto);

        categoryQueryDto = mapper.readValue(converter, CategoryQueryDto.class);


        return categoryQueryDto;


    } else {
        return null;
    }

}

POM

<dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-core-asl</artifactId>
     <version>1.9.13</version>
     <scope>test</scope>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM