我在Spring启动应用程序中有一个控制器,它使用Hibernate Validator验证输入。当我调用其余端点时,它会正确验证,但我的控制器建议没有捕获它,以自定义消息。我们只获得404空代码的代码。
@RestController
public class Controller {
@RequestMapping(method = RequestMethod.POST, path = RestPaths.LOAD_DATA)
public void loadCostCenterData(@RequestBody @Valid ClientDto dto) {
}
}
@RestControllerAdvice
public class WickesGlobalExceptionMapper extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity handleOtherUnexpectedException(Exception ex, WebRequest request) {
}
}
答案 0 :(得分:1)
这是一个使用ModelView而不是ResponseEntity的工作ControllerAdvise:
@ControllerAdvice
public class WickesGlobalExceptionMapper {
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView handleInvalidArgument(IllegalArgumentException ex) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setView(new MappingJackson2JsonView());
modelAndView.addObject("errorMessage", format("{0}", errorMessage));
return modelAndView;
}
}