无法使用 Spring 数据 REST 捕获 DataIntegrityViolationException
Couldn't catch DataIntegrityViolationException with Spring Data REST
尝试使用 ControllerAdvice 和 return 使用 Spring Boot v 1.3.3 和 Spring Data REST v 2.4.4 的自定义响应来处理 DataIntegrityViolationException。这是我的 类:
ExceptionControllerAdvice.java
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(DataIntegrityViolationException.class)
public ViolationResponse handleConflictException(DataIntegrityViolationException ex) throws Exception {
return new ViolationResponse(ex.getMessage());
}
ViolationResponse.java
public class ViolationResponse {
private String message;
public ViolationResponse(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我希望这个 return 409 HTTP 状态代码与消息
冲突
{"message" : "..."}
但是我得到的是 404 状态,而不是这个响应:
{
"timestamp": 1463382639043
"status": 404
"error": "Not Found"
"exception": "org.springframework.dao.DataIntegrityViolationException"
"message": "could not execute statement; SQL [n/a]; constraint [email_exists_constraint]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
"path": "/api/v1/register"
}
我在这里错过了什么?如何达到预期的效果?
编辑:
AuthController.java
@RestController
@RequestMapping("/api/v1")
public class AuthController {
...
@RequestMapping(value = "/register", method = RequestMethod.POST)
public User register(@Valid @RequestBody User user, BindingResult result) throws ValidationException {
if (result.hasErrors()) {
throw new ValidationException(result.getAllErrors());
}
return userRepository.save(user);
}
在 ExceptionControllerAdvice.java 中用 @ResponseBody 注释你的方法 handleConflictException。这将告诉 spring 从您的 return 对象创建一个 JSON。 @RestController 里面有@ResponseBody,@ControllerAdvice 里面没有。
尝试使用 ControllerAdvice 和 return 使用 Spring Boot v 1.3.3 和 Spring Data REST v 2.4.4 的自定义响应来处理 DataIntegrityViolationException。这是我的 类:
ExceptionControllerAdvice.java
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(DataIntegrityViolationException.class)
public ViolationResponse handleConflictException(DataIntegrityViolationException ex) throws Exception {
return new ViolationResponse(ex.getMessage());
}
ViolationResponse.java
public class ViolationResponse {
private String message;
public ViolationResponse(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我希望这个 return 409 HTTP 状态代码与消息
冲突{"message" : "..."}
但是我得到的是 404 状态,而不是这个响应:
{ "timestamp": 1463382639043 "status": 404 "error": "Not Found" "exception": "org.springframework.dao.DataIntegrityViolationException" "message": "could not execute statement; SQL [n/a]; constraint [email_exists_constraint]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement" "path": "/api/v1/register" }
我在这里错过了什么?如何达到预期的效果?
编辑:
AuthController.java
@RestController
@RequestMapping("/api/v1")
public class AuthController {
...
@RequestMapping(value = "/register", method = RequestMethod.POST)
public User register(@Valid @RequestBody User user, BindingResult result) throws ValidationException {
if (result.hasErrors()) {
throw new ValidationException(result.getAllErrors());
}
return userRepository.save(user);
}
在 ExceptionControllerAdvice.java 中用 @ResponseBody 注释你的方法 handleConflictException。这将告诉 spring 从您的 return 对象创建一个 JSON。 @RestController 里面有@ResponseBody,@ControllerAdvice 里面没有。