我有这个存储库
@RepositoryRestResource(collectionResourceRel = "party_category", path = "party_category")
public interface PartyCategoryRepository extends PagingAndSortingRepository<PartyCategory, Integer>
{
}
Run Code Online (Sandbox Code Playgroud)
当我试图坚持:
curl -X POST -H "Content-Type: application/json" http://localhost:8080/party_category -d "{\"description\":\"Mock description 3 modif\"}"
Run Code Online (Sandbox Code Playgroud)
我得到500错误而不是400:
{
"timestamp": 1438616019406,
"status": 500,
"error": "Internal Server Error",
"exception": "javax.validation.ConstraintViolationException",
"message": "Validation failed for classes[main.entity.party.PartyCategory] during persist time for groups [javax.validation.groups.Default, ]\\\nList of constraint violations:[\\\n\\\tConstraintViolationImpl{interpolatedMessage='no puede estar vacío', propertyPath=name, rootBeanClass=class main.entity.party.PartyCategory, messageTemplate='{org.hibernate.validator.constraints.NotBlank.message}'}\\\n]",
"path": "/party_category"
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能得到这样的友好回应?:
{
"field":"name",
"exception":"Field name cannot be null"
}
Run Code Online (Sandbox Code Playgroud)
如何返回400 http代码?
您似乎需要将特定的Exception映射到HTTP状态:
@Controller
public class ExceptionHandlingController {
// Convert a predefined exception to an HTTP Status code
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Data integrity violation") // 409
@ExceptionHandler(javax.validation.ConstraintViolationException.class)
public void error() {
// Nothing to do
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1292 次 |
| 最近记录: |