Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jackson检查可选字段 问题:_Java_Json_Jackson_Jax Rs - Fatal编程技术网

Java Jackson检查可选字段 问题:

Java Jackson检查可选字段 问题:,java,json,jackson,jax-rs,Java,Json,Jackson,Jax Rs,我正在JAX-RS项目中制作一个POJO(Person)并使用Jackson。 我想创建一个可选的字符串字段(如此人居住的国家),并能够检查其长度 我的测试 通过引用本文()我知道如何创建可选字段。但是如果我想用javax.validation.constraints.Pattern检查它的长度,如下所示: @Pattern(regexp = "(?:.{0,12})?") private final String country; 国家不能为空或不再存在 我尝试添加@Optional(org

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

我的测试 通过引用本文()我知道如何创建可选字段。但是如果我想用javax.validation.constraints.Pattern检查它的长度,如下所示:

@Pattern(regexp = "(?:.{0,12})?")
private final String country;
国家不能为空或不再存在

我尝试添加@Optional(
org.jvnet.hk2.annotations.Optional
),并指定类似于
私有最终可选国家的国家。我没有成功地使用这两种方法

我真正的POJO
import com.fasterxml.jackson.annotation.*;
导入com.fasterxml.jackson.annotation.jsonautodect.Visibility;
导入org.hibernate.validator.constraints.NotBlank;
导入javax.validation.constraints.Pattern;
@JsonAutoDetect(creatorVisibility=Visibility.ANY,fieldVisibility=Visibility.ANY)
@JsonPropertyOrder({Person.LAST_NAME,Person.FIRST_NAME,Person.COUNTRY})
@JsonIgnoreProperties(ignoreUnknown=true)
公共阶层人士{
/**
*toString方法的格式字符串
*/
私有静态最终字符串为\u字符串\u格式=“%S%S%S”;
/**
*姓氏的JSON属性名
*/
静态最终字符串LAST\u NAME=“lastName”;
/**
*第一个名称的JSON属性名
*/
静态最终字符串FIRST\u NAME=“firstName”;
/**
*国家/地区的JSON属性名称
*/
静态最终字符串COUNTRY=“COUNTRY”;
/**
*此人的姓氏
*/
@不空白
私有最终字符串lastName;
/**
*这个人的名字
*/
@不空白
私有最终字符串名;
/**
*个人的国家
*/
@模式(regexp=“(?:.{0,12}”))
私人国家;
/**
*返回一个新的{@code Person},其属性由参数初始化。
*
*@param lastName此人的姓氏({@link#lastName})
*@param firstName此人的名字({@link#firstName})
*@param country此人居住的国家({@link#country})
*/
//仅由Jackson用于JSON数据反序列化
@抑制警告(“未使用”)
@JsonCreator
个人(@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();
}
/**
*返回一个新的{@code Person},其属性已从另一个初始化。
*
*@param person用于创建新实例的实例
*/
人(人){
this.lastName=person.lastName;
this.firstName=person.firstName;
this.country=person.country;
}
/**
*返回{@code Person}:{@link#lastName}{@link#firstName}{@link#country}的文本表示形式。
*
*{@code lastName}被转换为大写,以提高人名的可读性。
*
*@返回{@code Person}的字符串表示形式
*/
@凌驾
公共字符串toString(){
返回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();
    }
}
有了它,你就不必关心它是否设置了,而且不管发生什么,正则表达式都会匹配

我希望这能有所帮助。

问题 问题是我在country上做了一个
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();
}
解决方案 我要感谢@Theodcoder的解决方案。我的
@Pattern
regex没有更改,但是构造函数
私人(…)
(@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();
    }
}

谢谢你有用的回答。我刚刚将解决方案的编写位置从构造函数
Person(Person-Person)
更改为构造函数
private Person(…)
(@JsonCreator)。“我要感谢@theodcoder的解决方案。”接受并更新他的答案,而不是发布你的答案…@CassioMazzochiMolin正如我在对他的答案的评论中提到的,我不得不修改他的答案,使之与我的问题来源相匹配。
@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();
    }
}