【问题标题】:Gson equivalent to Jackson @JsonInclude(JsonInclude.Include.NON_NULL)Gson 相当于 Jackson @JsonInclude(JsonInclude.Include.NON_NULL)
【发布时间】:2021-01-14 06:14:10
【问题描述】:

这实际上是对这个问题Recreate DTO class without field property instead of having it null using Gson/Jackson and Spring Boot 的跟进。我最初发布它是为了让它与 Gson 一起使用,但只能使用 @JsonInclude(JsonInclude.Include.NON_NULL) 与 Jackson 一起使用,但无法找到与 Gson 对应的等效项,因此我可以将其保留为项目的库。

尝试使用 @Expose(serialise=false, deserialise=false) 我有 @JsonInclude 注释或将该字段设置为 null 因为默认情况下 Gson 会忽略它,但它似乎没有这样做。

最后,我尝试完全删除 @Expose 注释,看看 Gson 是否会忽略它但也不起作用。

在此处粘贴问题的主要部分,并保留添加到原始帖子中的额外细节。

@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;
}

}

如果有人对如何做到这一点有任何其他想法,请。 非常感谢。

【问题讨论】:

    标签: java spring-boot serialization jackson gson


    【解决方案1】:

    不要序列化空字段(这是 Gson 序列化的默认行为)

    Employee employeeObj = new Employee(1, "John", "Smith", null);
                     
    Gson gson = new GsonBuilder()
            .setPrettyPrinting()
            .create(); 
     
    System.out.println(gson.toJson(employeeObj));
    

    输出:

    {
      "id": 1,
      "firstName": "John",
      "lastName": "Smith"
    }
    

    序列化空字段(JSON 输出中包含空值的自定义 Gson 序列化)

    Employee employeeObj = new Employee(1, "John", "Smith", null);
                     
    Gson gson = new GsonBuilder()
            .setPrettyPrinting()
            .serializeNulls()
            .create(); 
     
    System.out.println(gson.toJson(employeeObj));
    

    输出:

    {
      "id": 1,
      "firstName": "John",
      "lastName": "Smith",
      "emailId": null
    }
    

    【讨论】:

    • 您好,谢谢您的回答。由于 Gson 的默认行为没有发生并且必须在 .properties 文件中覆盖它,因此认为它有助于找出问题所在。
    【解决方案2】:

    似乎 Gson 的默认行为没有发生,因为 Spring Boot 使用 Jackson 作为默认序列化库。通过在application.properties 文件中粘贴下面的行来覆盖它已经为我解决了这个问题。

    spring.mvc.converters.preferred-json-mapper=gson
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      相关资源
      最近更新 更多