SpringMVC异常处理

springMVC统一异常处理:


1、利用springMVC提供的SimpleMappingExceptionResolver;
2、自定义异常处理器,实现HandlerExceptionResolver
3、通过注解@ExceptionHandler


  2 具体操作:

     统一异常操作

       1 spring-MVC配置文件:

          <bean  class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  
          <property name="defaultErrorView" value="error"></property> 

      2 contorller控制器:


          @RequestMapping("/simple")
public String addStudent(String name){
if(name==null){
throw new NullPointerException("用户名不能为空");
}
System.out.println(name);
return "success";

}

        自定义异常操作:

     1   spring-MVC配置文件:

            <bean class="com.zhidisort.spring.exception.ExceptionHandler">
  
         <property name="defaultViewName" value="error"></property>

           </bean> 

    2  异常处理类:

@Controller
@RequestMapping("/ex")
public class StudentException {
     
/**

* @return
*/
public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object handler,
Exception ex) {
   String xhr=req.getHeader("x-Requested-with");
   if(xhr==null || !xhr.contains("XMLHttpRequest") ){
    ModelAndView  mv=new ModelAndView();
   
mv.setViewName("error");
return mv;
   }
   
   
     try {
     resp.setCharacterEncoding("utf-8");

     resp.setContentType("application/json;charset=utf-8");
 PrintWriter  out=resp.getWriter();
 
 Map<String,Object> map=new HashMap<>();
 map.put("success", false);
 map.put("msg", ex.getMessage());
 
 ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(out, map);

 out.flush();
 out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

}

    

   3 contorller控制器:

      @RequestMapping("/tobean")
public String toBean(){
return "tobean";

}
@RequestMapping("/simple")
public String addStudent(String name){
if(name==null){
throw new NullPointerException("用户名不能为空");
}
System.out.println(name);
return "success";

}

       

   注解异常处理

     操作:

     @Controller
@RequestMapping("/ex")
public class StudentException {
     
/**

* @return
*/
@ExceptionHandler
public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object handler,
Exception ex) {
   String xhr=req.getHeader("x-Requested-with");
   if(xhr==null || !xhr.contains("XMLHttpRequest") ){
    ModelAndView  mv=new ModelAndView();
   
mv.setViewName("error");
return mv;
   }
   
   
     try {
     resp.setCharacterEncoding("utf-8");
     resp.setContentType("application/json;charset=utf-8");
 PrintWriter  out=resp.getWriter();
 
 Map<String,Object> map=new HashMap<>();
 map.put("success", false);
 map.put("msg", ex.getMessage());
 
 ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(out, map);

 out.flush();
 out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}
@RequestMapping("/tobean")
public String toBean(){
return "tobean";

}
@RequestMapping("/simple")
public String addStudent(String name){
if(name==null){
throw new NullPointerException("用户名不能为空");
}
System.out.println(name);
return "success";

}

}






















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值