我克隆了这个项目并将Hotel.java更改为:
@Entity
public class Hotel implements Serializable {
.
.
@javax.validation.constraints.NotNull
//@Column(nullable = false)
private String address;
.
.
}
Run Code Online (Sandbox Code Playgroud)
在发布帖子请求时:
curl -H "Content-Type: application/json" -X POST -d '{}' http://localhost:8080/api/hotels
Run Code Online (Sandbox Code Playgroud)
回应是:
{
"timestamp": 1479213670718,
"status": 500,
"error": "Internal Server Error",
"exception": "javax.validation.ConstraintViolationException",
"message": "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [sample.data.rest.domain.Hotel] during persist time for groups [javax.validation.groups.Default, ]\nList of constraint violations:[\n\tConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=address, rootBeanClass=class sample.data.rest.domain.Hotel, messageTemplate='{javax.validation.constraints.NotNull.message}'}\n]",
"path": "/api/hotels"
}
Run Code Online (Sandbox Code Playgroud)
然后我写了这个案例的测试:
@RunWith(SpringRunner.class) …Run Code Online (Sandbox Code Playgroud)