尝试使用 ExceptionHandler 捕获 @valid 时出错
我有一个带有这样的 REST 控制器的 Spring Web 应用程序:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public @ResponseBody ResponseEntity<Resp> do(@Valid @RequestBody(required = true) Body b){
我的对象具有如下验证:
public class Body implements Serializable {
@NotEmpty
@Max(value = 32)
private String nss;
我的 @ExceptionHandler 是这样的:
@ControllerAdvice
public class Handler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = { MethodArgumentNotValidException.class })
public ResponseEntity<RespError> invalidInput(RuntimeException ex) {
RespErrorresponse = new RespError();
return new ResponseEntity<RespError>(response, HttpStatus.BAD_REQUEST);
}
我试图捕获无效的异常,但每次我尝试启动应用程序时,我都会'我得到:
java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]: {public org.springframework.http.ResponseEntity com.algoApp.controller.RestResponseEntityExceptionHandler.invalidInput(java.lang.RuntimeException), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}
我应该如何实现处理程序来捕获此异常?
I have a Spring web app with a REST controller like this:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public @ResponseBody ResponseEntity<Resp> do(@Valid @RequestBody(required = true) Body b){
my object has validations like these:
public class Body implements Serializable {
@NotEmpty
@Max(value = 32)
private String nss;
and my @ExceptionHandler is this:
@ControllerAdvice
public class Handler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = { MethodArgumentNotValidException.class })
public ResponseEntity<RespError> invalidInput(RuntimeException ex) {
RespErrorresponse = new RespError();
return new ResponseEntity<RespError>(response, HttpStatus.BAD_REQUEST);
}
I'm trying to catch the not valid exception but everytime I'm trying to start the app, I'm getting:
java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]: {public org.springframework.http.ResponseEntity com.algoApp.controller.RestResponseEntityExceptionHandler.invalidInput(java.lang.RuntimeException), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}
how should I implement the handler to catch this exception instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的经验,匹配参数是有效的。
In my experience, matching arguments works.