杰克逊检查可选字段

Jackson check optional field

问题

我正在 JAX-RS 项目中创建一个 POJO(人)并使用 Jackson。
我想创建一个可选的字符串字段(例如此人居住的国家/地区)并能够检查其长度。

我的测试

参考这个 post (How to define optional json field using Jackson) 我知道如何制作一个可选字段。但是如果我想用 javax.validation.constraints.Pattern 检查它的长度,如下所示:

@Pattern(regexp = "(?:.{0,12})?")
private final String country;

国家 不能为 NULL 或不存在。

我尝试添加 @Optional (org.jvnet.hk2.annotations.Optional) 并指定 country 喜欢 private final Optional<String> country;。我用这两种方法都没有成功。

我的实际 POJO

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import org.hibernate.validator.constraints.NotBlank;

import javax.validation.constraints.Pattern;

@JsonAutoDetect(creatorVisibility = Visibility.ANY, fieldVisibility = Visibility.ANY)
@JsonPropertyOrder({Person.LAST_NAME, Person.FIRST_NAME, Person.COUNTRY})
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {

    /**
     * The format string for the toString method
     */
    private static final String TO_STRING_FORMAT = "%S %s %s";

    /**
     * JSON property name for the last name
     */
    static final String LAST_NAME = "lastName";

    /**
     * JSON property name for the first name
     */
    static final String FIRST_NAME = "firstName";

    /**
     * JSON property name for the country
     */
    static final String COUNTRY = "country";

    /**
     * The last name of the person
     */
    @NotBlank
    private final String lastName;

    /**
     * The first name of the person
     */
    @NotBlank
    private final String firstName;

    /**
     * The country of the person
     */
    @Pattern(regexp = "(?:.{0,12})?")
    private final String country;

    /**
     * Returns an new {@code Person} with its properties initialized from parameters.
     *
     * @param lastName the last name of the person ({@link #lastName})
     * @param firstName the first name of the person ({@link #firstName})
     * @param country the country where the person live ({@link #country})
     */
    // Only used by Jackson for the JSON data deserialization
    @SuppressWarnings("unused")
    @JsonCreator
    private Person(@JsonProperty(Person.LAST_NAME) String lastName, @JsonProperty(Person.FIRST_NAME) String firstName, @JsonProperty(Person.COUNTRY) String country) {
        this.lastName = lastName.trim();
        this.firstName = firstName.trim();
        this.country = country.trim();
    }

    /**
     * Returns a new {@code Person} with its properties initialized from another one.
     *
     * @param person the instance used to create the new one
     */
    Person(Person person) {
        this.lastName = person.lastName;
        this.firstName = person.firstName;
        this.country = person.country;
    }

    /**
     * Returns a textual representation of the {@code Person}: {@link #lastName} {@link #firstName} {@link #country}.
     * <p>
     * The {@code lastName} is converted to uppercase for better readability of the person's name.
     *
     * @return a string representation of the {@code Person}
     */
    @Override
    public String toString() {
        return String.format(Person.TO_STRING_FORMAT, this.lastName, this.firstName, this.country);
    }
}

在您的情况下,我可能会建议您不要为您的字段使用可选字段,而是验证您的国家/地区是否已设置并以这种方式处理。

private Person(@JsonProperty(Person.LAST_NAME) String lastName, @JsonProperty(Person.FIRST_NAME) String firstName, @JsonProperty(Person.COUNTRY) String country) {
    this.lastName = lastName.trim();
    this.firstName = firstName.trim();
    if(country == null) { //here you could also use the string empty or null check of apache or guava
        this.country = country;
    } else {
        this.country = country.trim();
    }
}

有了它,您就不必关心它是否设置了,无论如何正则表达式都会匹配。

希望对您有所帮助。

问题

问题是我在国家(可能是 NULL 值)上创建了 trim()。见下文:

@SuppressWarnings("unused")
@JsonCreator
private Person(@JsonProperty(Person.LAST_NAME) String lastName, @JsonProperty(Person.FIRST_NAME) String firstName, @JsonProperty(Person.COUNTRY) String country) {
    this.lastName = lastName.trim();
    this.firstName = firstName.trim();
    this.country = country.trim();
}

解决方案

我要感谢@TheOddCoder 的解决方案。我的 @Pattern 正则表达式没有改变,但构造函数 private Person(...) (@JsonCreator) 发生以下变化:

@SuppressWarnings("unused")
@JsonCreator
private Person(@JsonProperty(Person.LAST_NAME) String lastName, @JsonProperty(Person.FIRST_NAME) String firstName, @JsonProperty(Person.COUNTRY) String country) {
    this.lastName = lastName.trim();
    this.firstName = firstName.trim();
    if(country == null) {
        this.country = country;
    } else {
        this.country = country.trim();
    }
}