分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                     

ResponseStatus

ResponseStatus注解的使用非常简单,我们创建一个异常类,加上注解

package com.zj.exception;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.ResponseStatus;@ResponseStatus(value=HttpStatus.FORBIDDEN,reason="用户不匹配")public class UserNotMatchException extends RuntimeException{}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
 
     
  • ResponseStatus注解是修饰类的
  •  
  • 它有两个属性,value属性是http状态码,比如404,500等。reason是错误信息
  •  

写一个目标方法抛出该异常

    @RequestMapping("/testResponseStatus")    public String testResponseStatus(int i){        if(i==0)            throw new UserNotMatchException();        return "hello";    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

当传入参数i==0的时候将抛异常。下图是效果图
springmvc学习笔记(30)——ResponseStatus注解处理异常

 

使用了ResponseStatus注解之后,用户看到的异常界面正是我们自己定义的异常,而不再是一大堆用户看不懂的代码。

ResponseStatus修饰方法

讲到这里,我就想呵呵以下。ResponseStatus如果修饰目标方法,将会发生什么事咧?且看:

    @ResponseStatus(value=HttpStatus.FORBIDDEN,reason="用户名不匹配")    @RequestMapping("/testResponseStatus")    public String testResponseStatus(int i){        if(i==0)            throw new UserNotMatchException();        return "hello";    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

springmvc学习笔记(30)——ResponseStatus注解处理异常

 
     
  • 仔细看这张结果图中的访问路径,我传入的参数是i=1,正常来说是不应该抛异常的,可是它抛了。。它真的抛了。。
  •  
  • 结论:ResponseStatus修饰目标方法,无论它执行方法过程中有没有异常产生,用户都会得到异常的界面。而目标方法正常执行
  •  
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

springmvc学习笔记(30)——ResponseStatus注解处理异常
                     

ResponseStatus

ResponseStatus注解的使用非常简单,我们创建一个异常类,加上注解

package com.zj.exception;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.ResponseStatus;@ResponseStatus(value=HttpStatus.FORBIDDEN,reason="用户不匹配")public class UserNotMatchException extends RuntimeException{}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
 
     
  • ResponseStatus注解是修饰类的
  •  
  • 它有两个属性,value属性是http状态码,比如404,500等。reason是错误信息
  •  

写一个目标方法抛出该异常

    @RequestMapping("/testResponseStatus")    public String testResponseStatus(int i){        if(i==0)            throw new UserNotMatchException();        return "hello";    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

当传入参数i==0的时候将抛异常。下图是效果图
springmvc学习笔记(30)——ResponseStatus注解处理异常

 

使用了ResponseStatus注解之后,用户看到的异常界面正是我们自己定义的异常,而不再是一大堆用户看不懂的代码。

ResponseStatus修饰方法

讲到这里,我就想呵呵以下。ResponseStatus如果修饰目标方法,将会发生什么事咧?且看:

    @ResponseStatus(value=HttpStatus.FORBIDDEN,reason="用户名不匹配")    @RequestMapping("/testResponseStatus")    public String testResponseStatus(int i){        if(i==0)            throw new UserNotMatchException();        return "hello";    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

springmvc学习笔记(30)——ResponseStatus注解处理异常

 
     
  • 仔细看这张结果图中的访问路径,我传入的参数是i=1,正常来说是不应该抛异常的,可是它抛了。。它真的抛了。。
  •  
  • 结论:ResponseStatus修饰目标方法,无论它执行方法过程中有没有异常产生,用户都会得到异常的界面。而目标方法正常执行
  •  
           

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-08-01
  • 2021-11-18
  • 2022-02-04
  • 2021-12-27
猜你喜欢
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2021-04-22
  • 2021-10-17
相关资源
相似解决方案