@ControllerAdvice global exception handler

Define global exception handler using @ControllerAdvice

 

Package com.app; 

Import java.io.IOException;
 Import java.io.PrintWriter; 

Import javax.servlet.http.HttpServletResponse; 

Import org.springframework.web.bind.annotation.ControllerAdvice;
 Import org.springframework.web.bind. annotation.ExceptionHandler;
 Import org.springframework.web.multipart.MaxUploadSizeExceededException; 

/ ** 
 * ContorllerAdvice most common scenario is to use global exception handler 
 * ships with @ExceptionHandler @ModelAttribute and @InitBinder use 
 * as follows, when a single file exceeds the maximum size when the processing method corresponding custom 
 * 
 * <P> By the methods in default AN { @code @ControllerAdvice} Apply to Globally 
 * All the Controllers. 
 *
 * spring:
  servlet:
    multipart:
      max-file-size: 50KB  
 *
 */
@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void uploadException(MaxUploadSizeExceededException e, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/html;charset=utf-8");
    PrintWriter out = resp.getWriter();
    out.write("文件大小超出限制!");
    out.flush();
    out.close();
    }
}

 

Guess you like

Origin www.cnblogs.com/luffystory/p/12010494.html