杰克逊检查可选字段



问题

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

我的测试

通过参考这篇文章(如何使用杰克逊定义可选的 json 字段(,我知道如何制作可选字段。但是,如果我想用如下所示javax.validation.constraints.Pattern检查它的长度:

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

这个国家不能是空的或不再存在。

我尝试添加@Optional(org.jvnet.hk2.annotations.Optional(并指定国家/地区,例如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);
}
}

问题

问题是我在国家/地区进行了trim()(可能是 NULL 值(。见下文:

@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();
}
}

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

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();
}
}

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

我希望这有所帮助。

最新更新