Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gson相当于Jackson@JsonInclude(JsonInclude.Include.NON_NULL)_Java_Spring Boot_Serialization_Jackson_Gson - Fatal编程技术网

Java Gson相当于Jackson@JsonInclude(JsonInclude.Include.NON_NULL)

Java Gson相当于Jackson@JsonInclude(JsonInclude.Include.NON_NULL),java,spring-boot,serialization,jackson,gson,Java,Spring Boot,Serialization,Jackson,Gson,这实际上是对这个问题的跟进。我最初发布它是为了让它与Gson一起工作,但只能使用@JsonInclude(JsonInclude.Include.NON_NULL)与Jackson一起工作,但还没有找到与Gson相同的版本,所以我可以将其作为项目的库 尝试使用@Expose(serialise=false,deserialise=false),其中我有@JsonInclude注释,或者将该字段设置为null,因为默认情况下Gson会忽略这一点,但它似乎没有这样做 最后,我尝试从中完全删除@Exp

这实际上是对这个问题的跟进。我最初发布它是为了让它与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;
}

}
@服务
公共类CategoryQueryServiceImpl实现CategoryQueryService{
@自动连线
私人分类报告分类报告;
@自动连线
私有报告存储库;
ObjectMapper mapper=新的ObjectMapper();
@凌驾
public CategoryQueryTo getCategory(UUID)抛出JsonProcessingException{
if(categoryRepository.findById(id).isPresent()){
Category=categoryRepository.findById(id.get();
CategoryQueryDto CategoryQueryDto=新的CategoryQueryDto(category.getId(),category.getTitle());
Gson Gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
字符串转换器=gson.toJson(categoryQueryDto);
categoryQueryDto=gson.fromJson(转换器,categoryQueryDto.class);
//杰克逊
//字符串转换器=mapper.writeValueAsString(CategoryQueryTo);
//categoryQueryDto=mapper.readValue(转换器,categoryQueryDto.class);
返回类别querydto;
}否则{
返回null;
}
}
@AllArgsConstructor
@诺尔格构装师
@资料
公共类类别QueryTo{
@暴露()
私有UUID;
@暴露()
私有字符串标题;
//杰克逊
//@JsonInclude(JsonInclude.Include.NON_NULL)
私有列表报告=null;
公共类别QueryTo(UUID id,字符串标题){
this.id=id;
this.title=标题;
}
}
如果有人对如何做到这一点有任何其他想法,请。
非常感谢。

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

输出:

{
  "id": 1,
  "firstName": "John",
  "lastName": "Smith"
}
{
  "id": 1,
  "firstName": "John",
  "lastName": "Smith",
  "emailId": null
}
序列化空字段(自定义Gson序列化,JSON输出中包含空值)

输出:

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

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

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

您好,谢谢您的回答。了解问题所在非常有用,因为Gson的默认行为没有发生,必须在.properties文件中覆盖它。