1
我有這個REST控制器也應該處理異常。Spring REST多部分錯誤處理
@ExceptionHandler(MultipartException.class)
註釋不起作用explained。
因此,我正在實施HandlerExceptionResolver
,它基本上正在工作,但對於@ExceptionHandler
而言,不如REST和JSON響應便利。
我想返回我的自定義類ValidationReport
在resolveException
類似於@ExceptionHandler handleBadRequest
。我無法創建一個ModelAndView,其中包含一個ValidationReport
json響應。任何想法如何結合這兩種風格?
@RestController
class ValidationController implements HandlerExceptionResolver{
static Logger LOG = LoggerFactory.getLogger(ValidationController.class);
@RequestMapping(value="/validate", method=[POST])
public ValidationReport validate(MultipartFile file) {
LOG.info("received file ${file?.name}")
ValidationReport report = new ValidationReport();
return report
}
@ResponseStatus(BAD_REQUEST)
@ExceptionHandler(MultipartException.class)
@ResponseBody ValidationReport handleBadRequest(HttpServletRequest req, Exception ex) {
return new ValidationReport(USER_ERROR, "you should not upload files bigger then xx MB")
}
@Override
ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if (ex instanceof MultipartException){
response.sendError(BAD_REQUEST.value(),ex.message)
}
return null
}
}
我