[英]Jackson check optional field
我正在 JAX-RS 項目中制作一個 POJO(人)並使用 Jackson。
我想創建一個可選的 String 字段(如該人居住的國家/地區)並能夠檢查其長度。
通過參考這篇文章( 如何使用 Jackson 定義可選的 json 字段),我知道如何創建一個可選字段。 但是如果我想用javax.validation.constraints.Pattern
檢查它的長度,如下所示:
@Pattern(regexp = "(?:.{0,12})?")
private final String country;
國家不能為 NULL 或不再存在。
我嘗試添加@Optional ( org.jvnet.hk2.annotations.Optional
) 並指定國家,如private final Optional<String> country;
. 這兩種方法我都沒有成功。
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);
}
}
問題是我在國家(可能是 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();
}
}
在您的情況下,我可能會建議您不要為您的字段使用可選項,而是驗證您的國家/地區是否已設置並以這種方式處理。
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();
}
}
有了它,你就不必關心它是否設置了,不管怎樣,正則表達式都會匹配。
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.