当后台程序中抛出异常时,springboot默认将错误交由/error地址处理,处理方式可到org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController中查看。 通过现象可以发现springboot的处理方式很简单,只是单纯的跳转到一个错误界面,如图所示而该错误界面仅面向开发人员,对于用户来说是非常不友好的。所以我们需要将其进行跳转到有意义的界面进行异常提示。
接下来将会介绍四种不同的方式来处理不同的异常。
首先为了产生异常,在controller中手动制造了两个不同的异常,当访问/show和/show1时将会返回NullPointerException和ArithmeticException。
package com.myd.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* $DESCIRPTION
*
* @author zgp
* @create 2018 - 12 - 25 9:34
*/
@Controller
public class ShowController {
@RequestMapping("/show")
public ModelAndView show(){
String str = null;
str.length();
return new ModelAndView();
}
@RequestMapping("/show1")
public ModelAndView show1(){
int a = 10 / 0;
return new ModelAndView();
}
}
接下来开始对异常进行处理
package com.myd.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* $DESCIRPTION
*
* @author zgp
* @create 2018 - 12 - 25 9:34
*/
@Controller
public class ShowController {
@RequestMapping("/show")
public ModelAndView show(){
String str = null;
str.length();
return new ModelAndView();
}
@RequestMapping("/show1")
public ModelAndView show1(){
int a = 10 / 0;
return new ModelAndView();
}
@ExceptionHandler(value = {NullPointerException.class})
public ModelAndView nullPointerException(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error1");
return mv;
}
@ExceptionHandler(value = {ArithmeticException.class})
public ModelAndView arithmeticException(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error2");
return mv;
}
}
优势:具有特殊性,针对性强。
劣势:只对当前Controller的异常有效,全局无效
package com.com.myd.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/**
* $DESCIRPTION
*
* @author zgp
* @create 2018 - 12 - 25 10:00
*/
@ControllerAdvice
public class GlobalException {
@ExceptionHandler(value = {NullPointerException.class})
public ModelAndView nullPointerException(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error1");
return mv;
}
@ExceptionHandler(value = {ArithmeticException.class})
public ModelAndView arithmeticException(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error2");
return mv;
}
}
优势:可管控全局异常的抛出跳转。
劣势:代码冗长,重复代码过多,对每一种异常都需要一个方法来捕获处理
package com.com.myd.exception;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import java.util.Properties;
/**
* $DESCIRPTION
*
* @author zgp
* @create 2018 - 12 - 25 10:00
*/
@Configuration
public class GlobalException {
@Bean
public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
SimpleMappingExceptionResolver smer = new SimpleMappingExceptionResolver();
Properties p = new Properties();
p.setProperty("java.lang.NullPointerException", "error1");
p.setProperty("java.lang.ArithmeticException", "error2");
smer.setExceptionMappings(p);
return smer;
}
}
优势:是第二种异常处理方式的简化版。无需对各异常进行捕获处理。
劣势:因无法返回ModelAndView,所以无法传递异常消息到前端
package com.com.myd.exception;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* $DESCIRPTION
*
* @author zgp
* @create 2018 - 12 - 25 10:00
*/
@Configuration
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView mv = new ModelAndView();
if (e instanceof NullPointerException) {
mv.setViewName("error1");
} else if (e instanceof ArithmeticException) {
mv.setViewName("error2");
}
mv.addObject("error", e.toString());
return mv;
}
}
优势:代码简洁,全局有效,复用性强
最后需要提到的是你也可以直接在resource/tampletes目录下创建error.html文件。在不适用以上处理方式的情况下springboot一旦抛出异常将会直接跳转到自定义的error界面。当然在项目上该处理方式不推荐,除非对异常页面处理需求粒度没那么细才会使用,也算是最简单的一种处理方式了。
下载传送门:https://download.csdn.net/download/ko289830707/10874421