温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot2.0自适应效果错误响应的示例分析

发布时间:2021-06-24 09:34:44 来源:亿速云 阅读:183 作者:小新 栏目:编程语言

这篇文章主要介绍了Springboot2.0自适应效果错误响应的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

实现效果当访问thymeleaf渲染页面时,显示的是自定义的错误页面

当以接口方式访问时,显示的是自定义的json数据响应

1. 编写自定义异常

package cn.jfjb.crud.exception;

/**
 * @author john
 * @date 2019/11/24 - 9:48
 */
public class UserNotExistException extends RuntimeException {
  public UserNotExistException() {
    super("用户不存在");
  }
}

2. 自定义异常处理&返回定制json数据,转发到/error进行自适应响应效果处理

package cn.jfjb.crud.handler;

import cn.jfjb.crud.exception.UserNotExistException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author john
 * @date 2019/11/24 - 10:43
 */
@ControllerAdvice
public class MyExceptionHandler {

  @ExceptionHandler(UserNotExistException.class)
  public String handleException(Exception e, HttpServletRequest request) {
    Map<String, Object> map = new HashMap<>();
    //传入我们自己的错误状态码 4xx 5xx,否则就不会进入定制错误页面的解析流程
    /**
     * Integer statusCode = (Integer) request
     .getAttribute("javax.servlet.error.status_code");
     */
    request.setAttribute("javax.servlet.error.status_code", 400);
    map.put("code", "user.notexist");
    map.put("message", e.getMessage());

    //转发给错误处理器MyErrorAttributes
    request.setAttribute("ext", map);
    //转发到/error进行自适应响应效果处理
    return "forward:/error";
  }
}

3. 定制数据携带出去

出现错误以后,会来到/error请求,会被BasicErrorController处理,响应出去可以获取的数据是由getErrorAttributes得到的(是AbstractErrorController(ErrorController)规定的方法);

1、完全来编写一个ErrorController的实现类【或者是编写AbstractErrorController的子类】,放在容器中;

2、页面上能用的数据,或者是json返回能用的数据都是通过errorAttributes.getErrorAttributes得到;

容器中DefaultErrorAttributes.getErrorAttributes();默认进行数据处理的;

自定义ErrorAttributes

package cn.jfjb.crud.component;

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * @author john
 * @date 2019/11/24 - 12:13
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
  @Override
  public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
    Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
    //获取自定义处理异常传递的参数
    Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", 0);

    map.put("company", "atguigu");
    map.put("ext", ext);
    return map;
  }
}

4. 配置application.yml

server:
error:
include-exception: true

5. 编写4xx.html自定义错误页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>4xx</title>
</head>
<body>
<h2>status:[[${status}]]</h2>
<h3>timestamp:[[${timestamp}]]</h3>
<h3>exception:[[${exception}]]</h3>
<h3>message:[[${message}]]</h3>
</body>
</html>

6. 测试

package cn.jfjb.crud.controller;

import cn.jfjb.crud.exception.UserNotExistException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author john
 * @date 2019/11/22 - 19:38
 */
@Controller
public class HelloController {
  
  @RequestMapping({"/testException"})
  public String testException(@RequestParam("user") String user) {
    if (user != "aaa") {
      throw new UserNotExistException();
    }
    return "index";
  }
}

Springboot2.0自适应效果错误响应的示例分析

Springboot2.0自适应效果错误响应的示例分析

Springboot2.0自适应效果错误响应的示例分析

感谢你能够认真阅读完这篇文章,希望小编分享的“Springboot2.0自适应效果错误响应的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI