So che questo è vecchio, ma stavo cercando una soluzione a questo pure e non riuscivo a trovare nulla. Stiamo fornendo servizi RESTful usando Spring e stiamo facendo il caricamento dei file e non eravamo sicuri su come gestirlo. Sono venuto con la seguente e speriamo che sia utile a qualcuno:
Tutte le nostre eccezioni vengono gestite con annotazioni, quindi abbiamo set-up il nostro resolver gestore degli errori come questo:
@Configuration
public class MyConfig{
@Bean
public AnnotationMethodHandlerExceptionResolver exceptionResolver(){
final AnnotationMethodHandlerExceptionResolver resolver = new AnnotationMethodHandlerExceptionResolver();
resolver.setMessageConverters(messageConverters());
resolver;
}
}
Poi un comune classe che può gestire l'eccezione
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;
}
}
Poi abbiamo sottoclasse The Commons resolver multipart e implementare l'interfaccia 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);
}
}
Ricorda che impostando 'fileItems = Collections.EMPTY_LIST;', tutti i parametri di richiesta vengono scartati. In altre parole, 'request.getParameterMap()' sarà '{}'. –
Il commento di @Markus è particolarmente importante da tenere presente se uno dei parametri di richiesta è un token csrf. L'impostazione 'fileItems = Collections.EMPTY_LIST;' scarta il token CSRF, facendo in modo che il filtro CSRF consideri questa richiesta non valida. –