项目遇到的知识点

设置类上自定义注释模板。

Setting–>Editor–>File and Code Templates -->Includes–>File Header

HTTP状态码

HttpStatus = {
        //Informational 1xx  信息
        '100' : 'Continue',  //继续
        '101' : 'Switching Protocols',  //交换协议

        //Successful 2xx  成功
        '200' : 'OK',  //OK
        '201' : 'Created',  //创建
        '202' : 'Accepted',  //已接受
        '203' : 'Non-Authoritative Information',  //非权威信息
        '204' : 'No Content',  //没有内容
        '205' : 'Reset Content',  //重置内容
        '206' : 'Partial Content',  //部分内容

        //Redirection 3xx  重定向
        '300' : 'Multiple Choices',  //多种选择
        '301' : 'Moved Permanently',  //永久移动
        '302' : 'Found',  //找到
        '303' : 'See Other',  //参见其他
        '304' : 'Not Modified',  //未修改
        '305' : 'Use Proxy',  //使用代理
        '306' : 'Unused',  //未使用
        '307' : 'Temporary Redirect',  //暂时重定向

        //Client Error 4xx  客户端错误
        '400' : 'Bad Request',  //错误的请求
        '401' : 'Unauthorized',  //未经授权
        '402' : 'Payment Required',  //付费请求
        '403' : 'Forbidden',  //禁止
        '404' : 'Not Found',  //没有找到
        '405' : 'Method Not Allowed',  //方法不允许
        '406' : 'Not Acceptable',  //不可接受
        '407' : 'Proxy Authentication Required',  //需要代理身份验证
        '408' : 'Request Timeout',  //请求超时
        '409' : 'Conflict',  //指令冲突
        '410' : 'Gone',  //文档永久地离开了指定的位置
        '411' : 'Length Required',  //需要Content-Length头请求
        '412' : 'Precondition Failed',  //前提条件失败
        '413' : 'Request Entity Too Large',  //请求实体太大
        '414' : 'Request-URI Too Long',  //请求URI太长
        '415' : 'Unsupported Media Type',  //不支持的媒体类型
        '416' : 'Requested Range Not Satisfiable',  //请求的范围不可满足
        '417' : 'Expectation Failed',  //期望失败

        //Server Error 5xx  服务器错误
        '500' : 'Internal Server Error',  //内部服务器错误
        '501' : 'Not Implemented',  //未实现
        '502' : 'Bad Gateway',  //错误的网关
        '503' : 'Service Unavailable',  //服务不可用
        '504' : 'Gateway Timeout',  //网关超时
        '505' : 'HTTP Version Not Supported'  //HTTP版本不支持

自定义异常

//定义在找不到资源时抛出该异常
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException{
	public NotFoundException() {super();}
    public NotFoundException(String message) {super(message);}
    public NotFoundException(String message, Throwable cause) {super(message, cause);}
}

2、使用ControllerAdvice进行全局的异常处理

//定义一个的普通类
@ControllerAdvice
public class ControllerExceptionHandler(){

 	//先获取logger对象
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

	//定义处理异常方法,参数为要处理的类
	@ExceptionHandler(Exception.class)//表示所有类都被处理
	public ModelAndView exceptionHandler(HttpServletRequest request,Exception e) throws Exception {
		//控制台打印请求地址和异常信息
		logger.error("Request URL : {} , Exception : {}", request.getRequestURL(),e);
		//AnnotationUtils工具类可以找到异常的注解,并抛出此异常
		  if(AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
            	throw e;
        }
		   ModelAndView mv = new ModelAndView();
		   //跳转错误页面
		   mv.setViewName("error/error");	       
       	   return mv;
	} 
}

拦截器相关

@Pointcut("execution(* com.qzjblog.blog.web..*.*(..))")
切面拦截时一定要注意,不能拦截全部类,只需要拦截Controller所在的包即可,不然就会将拦截器等全部拦截掉,还会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值