Tôi biết điều này là cũ, nhưng tôi đã tìm kiếm một giải pháp cho điều này là tốt và không thể tìm thấy bất cứ điều gì. Chúng tôi đang cung cấp các dịch vụ RESTful bằng Spring và chúng tôi đang thực hiện tải lên tệp và không chắc chắn cách xử lý việc này. Tôi đã đưa ra sau và hy vọng nó sẽ có ích cho ai đó:
Tất cả các trường hợp ngoại lệ của chúng tôi được xử lý với các chú thích, vì vậy chúng tôi có giải quyết xử lý lỗi của chúng tôi thiết lập như thế này:
@Configuration
public class MyConfig{
@Bean
public AnnotationMethodHandlerExceptionResolver exceptionResolver(){
final AnnotationMethodHandlerExceptionResolver resolver = new AnnotationMethodHandlerExceptionResolver();
resolver.setMessageConverters(messageConverters());
resolver;
}
}
Sau đó, một chung lớp có thể xử lý các ngoại lệ
public class MultipartExceptionHandler
{
@ExceptionHandler(MaxUploadSizeExceededException.class)
@ResponseStatus(value = HttpStatus.PRECONDITION_FAILED)
@ResponseBody
protected CustomError handleMaxUploadSizeExceededException(final HttpServletRequest request,
final HttpServletResponse response, final Throwable e)
throws IOException
{
logger.error(e);
CustomError c = new CustomErrorMaxFileSize("Max file size exceeded", MAX_FILE_SIZE);
return c;
}
@ExceptionHandler(MultipartException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
protected CustomError handleGenericMultipartException(final HttpServletRequest request,
final HttpServletResponse response, final Throwable e)
throws IOException
{
logger.error(e);
CustomError c = new CustomErrorGeneric("There was a problem with the upload");
return c;
}
}
Sau đó chúng tôi phân lớp các commons nhiều phần dữ liệu thống xử lý và thực hiện các giao diện HandlerExceptionResolver
@Component(value="multipartResolver") // Spring expects this name
public class MyMultipartResolver extends CommonsMultipartResolver implements HandlerExceptionResolver
{
// This is the Spring bean that handles exceptions
// We defined this in the Java configuration file
@Resource(name = "exceptionResolver")
private AnnotationMethodHandlerExceptionResolver exceptionResolver;
// The multipart exception handler with the @ExceptionHandler annotation
private final MultipartExceptionHandler multipartExceptionHandler = new MultipartExceptionHandler();
// Spring will call this when there is an exception thrown from this
// multipart resolver
@Override
public ModelAndView resolveException(
final HttpServletRequest request,
final HttpServletResponse response,
final Object handlerParam,
final Exception ex)
{
// Notice that we pass this.multipartExceptionHandler
// and not the method parameter 'handlerParam' into the
// exceptionResolver. We do this because the DispatcherServlet
// doDispatch() method calls checkMultipart() before determining
// the handler for the request. If doing the multipart check fails
// with a MultipartException, Spring will never have a reference
// to the handler and so 'handlerParam' will be null at this point.
return exceptionResolver.resolveException(request, response, this.multipartExceptionHandler, ex);
}
}
Lưu ý rằng bằng cách đặt 'fileItems = Collections.EMPTY_LIST;', tất cả các tham số yêu cầu đều bị loại bỏ. Nói cách khác, 'request.getParameterMap()' sẽ là '{}'. –
Nhận xét của @Markus đặc biệt quan trọng cần lưu ý nếu một trong các tham số yêu cầu là mã thông báo csrf. Đặt 'fileItems = Collections.EMPTY_LIST;' loại bỏ mã thông báo CSRF, khiến bộ lọc CSRF xử lý yêu cầu này là không hợp lệ. –