我想在使用約束條件註釋反序列化時驗證json,但它似乎不起作用。例如使用約束條件驗證json註釋不起作用
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class SimpleFooRequest {
@NotNull
@JsonProperty(value = "count")
@Min(value = 100,message = "must be min 10")
private Integer count;
@NotNull
private String name;
public String getName() {return name; }
public void setName(String name) { this.name = name; }
public Integer getCount() {return count;}
public void setCount(Integer count) {this.count = count;}
}
試圖解編JSON
@Test
public void testFooRequest() throws Exception
{
String fooJson = {"count":-10}
ObjectMapper mapper = new ObjectMapper()
SimpleFooRequest request = mapper.readValue(fooJson,SimpleFooRequest.class);
assert request.getCount().equals(-10);//expected fail but it pass !!!
}
似乎傳遞雖然我預計,斷言將失敗伴隨計數最小值限制時
。我也試圖把在制定者的註釋,但得到了同樣的結果
我有<mvc:annotation-driven />
和hibernate-validator-4.3.1.Final.jar
在我的依賴 我知道我可以創建自定義的驗證,但我認爲,對於簡單的驗證,我可以使用的註解。 我需要做些什麼才能使它工作?