GSON DTO 序列化为空

问题描述 投票:0回答:1
    @Bean
    public GsonBuilder gsonBuilder(List <GsonBuilderCustomizer> customizers) {
        GsonBuilder builder = new GsonBuilder();
        customizers.forEach((c) -> c.customize(builder));
        builder
                .serializeNulls()
                .excludeFieldsWithoutExposeAnnotation()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

}
@AllArgsConstructor
@Getter
@Setter
@NoArgsConstructor
public class ModelStepInfo {

    public boolean isSuccessed;
    public String description;
    public LocalDateTime startTime;
    public LocalDateTime endTime;
}
gson.toJson(new ModelStepInfo(false, "dasdsada", LocalDateTime.now(), LocalDateTime.now()))

打印:{} 版本:2.10.1 为什么它出来是空的? 任何解决方案? 我不知道怎么了。

gson
1个回答
0
投票

问题很可能是你在使用

excludeFieldsWithoutExposeAnnotation()
,但是你的字段都没有
@Expose
注解,所以都被忽略了

© www.soinside.com 2019 - 2024. All rights reserved.