我正在使用Spring 3.2.0。根据{{3}}回答,我在带注释的控制器中使用相同的方法来实现HandlerExceptionResolver
接口,例如,
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
Map<String, Object> model = new HashMap<String, Object>(0);
if (exception instanceof MaxUploadSizeExceededException) {
model.put("msg", exception.toString());
model.put("status", "-1");
} else {
model.put("msg", "Unexpected error : " + exception.toString());
model.put("status", "-1");
}
return new ModelAndView("admin_side/ProductImage");
}
和Spring配置包括,
<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10000</value>
</property>
</bean>
当文件大小超过时,应该调用前面的方法,它应该自动处理异常,但根本不会发生。即使发生异常,也不会调用方法resolveException()
。处理此异常的方法是什么?我错过了什么吗?
同样的事情也被指定为this。我不确定为什么它在我的情况下不起作用。
我已经使用here尝试了following approach,但它也没有用。
package exceptionhandler;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public final class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {MaxUploadSizeExceededException.class})
protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {
String bodyOfResponse = "This should be application specific";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
}
}
我还尝试将详细信息 - Exception
。
@ExceptionHandler(value={Exception.class})
在任何情况下都不会调用方法ResponseEntity()
。
通常,如果可能,我希望按控制器基础(控制器级别)处理此异常。为此,一个@ExceptionHandler
带注释的方法应仅对该特定控制器是活动的,而不是对整个应用程序是全局的,因为我的应用程序中只有少数网页处理文件上载。当引起此异常时,我只想在当前页面上显示用户友好的错误消息,而不是重定向到web.xml
文件中配置的错误页面。如果这甚至不可行,那么无论如何都应该处理这个例外而没有我刚才表达的任何自定义要求。
这两种方法都不适用于我。我找不到更多关于处理这个例外的事情。是否需要在XML文件或其他地方进行其他配置?
在抛出异常后我得到的内容可以在以下@ControllerAdvice
中看到。
答案 0 :(得分:8)
根据您发布的堆栈跟踪,在请求到达调度程序servlet之前,抛出了MaxUploadSizeExceeded
异常。因此,不会调用异常处理程序,因为在抛出异常时,目标控制器尚未确定。
如果您查看堆栈跟踪,您可以看到HiddenHttpMethodFilter
中的异常会获取您的multipart-request的所有参数 - 以及您的“to big”upload-data参数。
您的控制器处理多部分上传需要HiddenHttpMethodFilter
吗?如果没有,请从上传处理控制器中排除此过滤器。
答案 1 :(得分:3)
您可以将CommonsMultipartResolver解析Lazily属性配置为true,如下所示:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="resolveLazily" value="true"/>
</bean>
答案 2 :(得分:2)
根据Dirk Lachowski发布的answer,我从HiddenHttpMethodFilter
中排除了一些用于多部分上传的网页。
HiddenHttpMethodFilter
最初提供了类似/*
的网址格式。因此,将这些页面移动到单独的目录/文件夹中并指定不同的URL模式(如/xxx/*
)非常繁琐。为了避免这种情况,我在自己的类中继承了OncePerRequestFilter
并排除了用于多部分上传的页面,这些页面按预期工作,在当前页面上显示用户友好的错误消息。
package filter;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
public final class HiddenHttpMethodFilter extends OncePerRequestFilter {
/**
* Default method parameter: <code>_method</code>
*/
public static final String DEFAULT_METHOD_PARAM = "_method";
private String methodParam = DEFAULT_METHOD_PARAM;
/**
* Set the parameter name to look for HTTP methods.
*
* @see #DEFAULT_METHOD_PARAM
*/
public void setMethodParam(String methodParam) {
Assert.hasText(methodParam, "'methodParam' must not be empty");
this.methodParam = methodParam;
}
private boolean excludePages(String page) {
//Specifically, in my case, this many pages so far have been excluded from processing avoiding the MaxUploadSizeExceededException in this filter. One could use a RegExp or something else as per requirements.
if (page.equalsIgnoreCase("Category.htm") || page.equalsIgnoreCase("SubCategory.htm") || page.equalsIgnoreCase("ProductImage.htm") || page.equalsIgnoreCase("Banner.htm") || page.equalsIgnoreCase("Brand.htm")) {
return false;
}
return true;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String servletPath = request.getServletPath();
if (excludePages(servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length()))) {
String paramValue = request.getParameter(this.methodParam);
//The MaxUploadSizeExceededException was being thrown at the preceding line.
if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
HttpServletRequest wrapper = new filter.HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
filterChain.doFilter(wrapper, response);
} else {
filterChain.doFilter(request, response);
}
} else {
filterChain.doFilter(request, response);
}
}
/**
* Simple {@link HttpServletRequest} wrapper that returns the supplied
* method for {@link HttpServletRequest#getMethod()}.
*/
private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
private final String method;
public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
super(request);
this.method = method;
}
@Override
public String getMethod() {
return this.method;
}
}
}
在我的web.xml
文件中,指定了此过滤器 - filter.HiddenHttpMethodFilter
,而不是org.springframework.web.filter.HiddenHttpMethodFilter
,如下所示。
<filter>
<filter-name>multipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>multipartFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>filter.HiddenHttpMethodFilter</filter-class>
<!--<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> This was removed replacing with the preceding one-->
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我仍然希望有一个公平的方法来处理有关的例外以及org.springframework.web.filter.HiddenHttpMethodFilter
答案 3 :(得分:1)
您的@ExcpetionHandler无法正常工作,因为这些带注释的方法只允许返回类型的ModelAndView或String,这是我记忆中的。有关详细信息,请参阅this posting。
答案 4 :(得分:1)
我的解决方案:首先为实现HandlerExceptionResolver的类定义bean。
<bean id="classForBeanException" class="XXXX.path.To.classForBeanException" />
答案 5 :(得分:1)
在处理Exception的ControllerAdvice中你可以拥有这样的代码。它对我有用。这是在春季4.0 +
@ExceptionHandler(Exception.class)
public @ResponseBody BaseResponse onException(Exception e, HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
BaseResponse resp = new BaseResponse();
if(e instanceof MaxUploadSizeExceededException){
resp.setCode(FileUploadFailed.SIZE_EXCEED);
resp.setMessage("Maximum upload size exceeded");
}
return resp;
}
答案 6 :(得分:0)
无论如何,要获取在管理MaxUploadSizeExceededException的ExceptionHandler中的控制器方法中定义的@RequestParam参数吗?似乎在进入Controller方法之前会被抛出。