假设我有一个简单的POJO,如下面注释了Jackson 2.1和Hibernate Validator 4.3.1注释:
final public class Person {
@JsonProperty("nm")
@NotNull
final public String name;
public Person(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我将这样的JSON发送到Web服务:
{"name": null}
Run Code Online (Sandbox Code Playgroud)
报告ConstraintViolation时,Hibernate使用类成员标识符"name"而不是JsonProperty注释值.有谁知道是否可以让Hibernate Validator查看类的注释并使用该值代替?
我正在使用JBoss-7.1和RESTEasy开发一个简单的RESTFul服务.我有一个名为CustomerService的REST服务,如下所示:
@Path(value="/customers")
@ValidateRequest
class CustomerService
{
@Path(value="/{id}")
@GET
@Produces(MediaType.APPLICATION_XML)
public Customer getCustomer(@PathParam("id") @Min(value=1) Integer id)
{
Customer customer = null;
try {
customer = dao.getCustomer(id);
} catch (Exception e) {
e.printStackTrace();
}
return customer;
}
}
Run Code Online (Sandbox Code Playgroud)
当我点击URL http:// localhost:8080/SomeApp/customers/-1时, @ MIN约束将失败并在屏幕上显示堆栈跟踪.
有没有办法捕获这些验证错误,以便我可以准备一个带有正确错误消息的xml响应并显示给用户?