You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求响应反序列化正确处理:如何限制字段长度?

如何在Jackson反序列化时限制字符串字段长度到200字符?

针对你的GitHubAdditionalResponse类,要把JSON中body字段映射为description时自动截断到200字符,以下是几种实用方案:

方案一:自定义Jackson反序列化器

编写专门的反序列化器,在反序列化过程中直接处理字符串长度:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;

public class StringLengthLimitDeserializer extends StdDeserializer<String> {
    private static final int MAX_LENGTH = 200;

    public StringLengthLimitDeserializer() {
        super(String.class);
    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String value = p.getText();
        if (value == null) {
            return null;
        }
        return value.length() > MAX_LENGTH ? value.substring(0, MAX_LENGTH) : value;
    }
}

然后在description字段上添加注解指定使用该反序列化器:

@JsonProperty("body")
@JsonDeserialize(using = StringLengthLimitDeserializer.class)
private String description;

方案二:重写Setter方法处理

利用Lombok特性,手动编写setDescription方法覆盖自动生成的版本,在赋值时截断字符串:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class GitHubAdditionalResponse {
    @JsonProperty("title")
    private String title;

    @JsonProperty("user")
    private User user;

    @JsonProperty("updated_at")
    private OffsetDateTime updatedAt;

    @JsonProperty("body")
    private String description;

    @JsonIgnore
    private String type;

    // 手动编写setter方法,覆盖Lombok生成的默认实现
    public void setDescription(String description) {
        if (description != null && description.length() > 200) {
            this.description = description.substring(0, 200);
        } else {
            this.description = description;
        }
    }

    public record User(@JsonProperty("login") String login) {
    }
}

这种方式无需额外编写反序列化器,逻辑直接内嵌在实体类中,适合简单场景。

方案三:通过构造方法处理

如果依赖构造方法初始化对象,可在构造阶段直接处理字符串长度:

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class GitHubAdditionalResponse {
    private String title;
    private User user;
    private OffsetDateTime updatedAt;
    private String description;
    @JsonIgnore
    private String type;

    // 自定义构造方法,配合@JsonCreator指定参数映射
    @JsonCreator
    public GitHubAdditionalResponse(
            @JsonProperty("title") String title,
            @JsonProperty("user") User user,
            @JsonProperty("updated_at") OffsetDateTime updatedAt,
            @JsonProperty("body") String description) {
        this.title = title;
        this.user = user;
        this.updatedAt = updatedAt;
        // 处理description长度
        if (description != null && description.length() > 200) {
            this.description = description.substring(0, 200);
        } else {
            this.description = description;
        }
        this.type = null;
    }

    public record User(@JsonProperty("login") String login) {
    }
}

这种方式把截断逻辑放在对象创建时完成,适合依赖构造方法初始化的场景。

内容的提问来源于stack exchange,提问作者Capybara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:27:20