将 JSON 映射到 POJO 时的 Jackson 验证
Jackson Validation when mapping JSON to POJO
首先我必须承认,我对 Web 服务和 DropWizard 框架完全陌生。
我目前正在执行 POST 请求并尝试将 JSON body 解析为 POJO。
当 JSON 为所需格式时,这非常有效。但是,当 JSON 中的某个键丢失时,我的 POJO 求助于默认值。
我的问题是,如果缺少 JSON 键,是否可以检查所有值的 JSON 或让 Jackson 抛出某种异常?
public class PlacedBetDecimal extends PlacedBet {
private double odds;
// Empty constructor to please Jackson
public PlacedBetDecimal() {
}
public PlacedBetDecimal(@JsonProperty("bet_id") long bet_id,
@JsonProperty("stake") int stake,
@JsonProperty("odds") double odds) {
super.bet_id = bet_id;
super.stake = stake;
this.odds = odds;
}
public double getOdds() {
return odds;
}
@Override
public String toString() {
return "PlacedBetFractional{" +
"bet_id="+super.bet_id+
"stake="+super.stake+
"odds=" + odds +
'}';
}
}
body中的JSON如下:
{
"bet_id": 1,
"odds": 11.0,
"stake": 10
}
如果出于某种原因,有人提供了 body 具有:
{
"odds": 11.0,
"stake": 10
}
然后我希望能够捕捉到这个而不是 Jackson 自动将 bet_id 填充为 0。
我们将不胜感激任何帮助。
而不是使用 primitives
,而是使用它们相应的类型包装器,例如Double
而不是 double
,并使用像 @NotNull
.
这样的 bean 验证约束来标记它们
My question is, is there anyway of either checking the JSON for all
values or having Jackson through some sort of exception if a JSON key
is missing?
在我看来,向您的 POJO
添加 Bean 验证约束,然后对传入的表示执行验证。如果至少存在一次约束违规,Dropwizard 将 return 一个 422 Unprocessable
实体响应。
假设你有一个 Person
比如:
public class Person {
@NotEmpty // ensure that name isn't null or blank
private String name;
@NotNull @Min(18) // at least 18 years old!
private Integer age;
// getters and setters
}
然后,在我们的资源 class 中,我们可以将 @Valid
或 @Validated
注释添加到 Person
:
@PUT
public Person replace(@Valid Person person) {
// do stuff
}
如果缺少 name 或 age 字段,Dropwizard 将 return 422 Unprocessable
实体响应详细信息验证错误。
首先我必须承认,我对 Web 服务和 DropWizard 框架完全陌生。
我目前正在执行 POST 请求并尝试将 JSON body 解析为 POJO。
当 JSON 为所需格式时,这非常有效。但是,当 JSON 中的某个键丢失时,我的 POJO 求助于默认值。
我的问题是,如果缺少 JSON 键,是否可以检查所有值的 JSON 或让 Jackson 抛出某种异常?
public class PlacedBetDecimal extends PlacedBet {
private double odds;
// Empty constructor to please Jackson
public PlacedBetDecimal() {
}
public PlacedBetDecimal(@JsonProperty("bet_id") long bet_id,
@JsonProperty("stake") int stake,
@JsonProperty("odds") double odds) {
super.bet_id = bet_id;
super.stake = stake;
this.odds = odds;
}
public double getOdds() {
return odds;
}
@Override
public String toString() {
return "PlacedBetFractional{" +
"bet_id="+super.bet_id+
"stake="+super.stake+
"odds=" + odds +
'}';
}
}
body中的JSON如下:
{
"bet_id": 1,
"odds": 11.0,
"stake": 10
}
如果出于某种原因,有人提供了 body 具有:
{
"odds": 11.0,
"stake": 10
}
然后我希望能够捕捉到这个而不是 Jackson 自动将 bet_id 填充为 0。
我们将不胜感激任何帮助。
而不是使用 primitives
,而是使用它们相应的类型包装器,例如Double
而不是 double
,并使用像 @NotNull
.
My question is, is there anyway of either checking the JSON for all values or having Jackson through some sort of exception if a JSON key is missing?
在我看来,向您的 POJO
添加 Bean 验证约束,然后对传入的表示执行验证。如果至少存在一次约束违规,Dropwizard 将 return 一个 422 Unprocessable
实体响应。
假设你有一个 Person
比如:
public class Person {
@NotEmpty // ensure that name isn't null or blank
private String name;
@NotNull @Min(18) // at least 18 years old!
private Integer age;
// getters and setters
}
然后,在我们的资源 class 中,我们可以将 @Valid
或 @Validated
注释添加到 Person
:
@PUT
public Person replace(@Valid Person person) {
// do stuff
}
如果缺少 name 或 age 字段,Dropwizard 将 return 422 Unprocessable
实体响应详细信息验证错误。