ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot2.0自适应效果错误响应

2019-11-24 12:57:15  阅读:223  来源: 互联网

标签:map springboot2.0 错误 error springframework 响应 org import public


实现效果当访问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>
<h1>status:[[${status}]]</h1>
<h2>timestamp:[[${timestamp}]]</h2>
<h2>exception:[[${exception}]]</h2>
<h2>message:[[${message}]]</h2>
</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";
    }
}

标签:map,springboot2.0,错误,error,springframework,响应,org,import,public
来源: https://www.cnblogs.com/ifme/p/11921980.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有