dropwizard验证未知未知属性

时间:2015-09-18 19:23:48

标签: validation jackson dropwizard

我有以下课程,我在资源

中作为输入
@JsonIgnoreProperties(ignoreUnknown = false)
    public class InputRequest {
@NotEmpty
private List<String> names;

private DateTime startDate;

private DateTime endDate;

@ValidationMethod(message="startDate should be less than endDate")
public boolean isValidDates() {
    return startDate.isBefore(endDate);
}

@ValidationMethod(message = "one of the names is not valid")
public boolean isValidNames() {
    //do something
}

public List<String> getNames() {
    return names;
}

public void setNames(List<String> names) {
    this.names = names;
}

public DateTime getStartDate() {
    return startDate;
}

public void setStartDate(DateTime startDate) {
    this.startDate = startDate;
}

public DateTime getEndDate() {
    return endDate;
}

public void setEndDate(DateTime endDate) {
    this.endDate = endDate;
}

}

资源类如下

    public Response getData(@Auth String userId, @Valid InputRequest request) {

但是,以下输入不会导致任何异常

{"names":["somename"],"startDate":1427155200000,"endDate":1427846400000, "x":"a"}

有人可以告诉我这里做错了吗?

编辑:此外,如果我发送以下内容,它将通过

{"names":["somename"],"startDate":1427155200000}

如果缺少某些字段,我希望它失败

1 个答案:

答案 0 :(得分:0)

    bootstrap.getObjectMapperFactory().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

为我工作