与杰克逊而不是格森一起尝试,并遇到了同样的问题.解决方案在我想仅在不为null且有效的情况下显示的字段上使用了@JsonInclude(JsonInclude.Include.NON_NULL).@AllArgsConstructor@NoArgsConstructor@Datapublic 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;}}这是我与杰克逊的控制人@Servicepublic class CategoryQueryServiceImpl implements CategoryQueryService {@Autowiredprivate CategoryRepository categoryRepository;@Autowiredprivate ReportRepository reportRepository;ObjectMapper mapper = new ObjectMapper();@Overridepublic 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>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@Datapublic 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; }}@Overridepublic 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.UPDATEHere is my code on Github https://github.com/francislainy/gatling_tool_backendTried 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@Datapublic 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@Servicepublic class CategoryQueryServiceImpl implements CategoryQueryService {@Autowiredprivate CategoryRepository categoryRepository;@Autowiredprivate ReportRepository reportRepository;ObjectMapper mapper = new ObjectMapper();@Overridepublic 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> 这篇关于使用Gson/Jackson和Spring Boot重新创建没有字段属性的DTO类,而不是将其设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 01:16