当前位置: 首页 > 工具软件 > JsonSerialize > 使用案例 >

使用@JsonSerialize,将后端参数,转成前端需要的格式

左丘宜然
2023-12-01

需求:将long类型的版本号,转成字符串

1.字段上加注解


    @JsonSerialize(using = FileVersion.class)
    private Long fileVersion;

2.增加自定义的类

    public static class FileVersion extends JsonSerializer<Long> {

        @Override
        public void serialize(Long aLong, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString("V" + aLong + ".0");
        }
    }

完整代码:


@Data
public class DrillFileLogPo extends Model<DrillFileLogPo> {

    private static final long serialVersionUID = 1L;


    @ApiModelProperty(value = "版本")
    @JsonSerialize(using = FileVersion.class)
    private Long fileVersion;

    @ApiModelProperty(value = "创建人名称")
    private String creator;

    @ApiModelProperty(value = "创建人Id")
    private Long creatorId;

    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime insertTime;
 
    public static class FileVersion extends JsonSerializer<Long> {

        @Override
        public void serialize(Long aLong, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString("V" + aLong + ".0");
        }
    }


}

 

 类似资料: