• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Springboot2.0自适应效果错误响应过程解析

springboot 搞代码 4年前 (2022-01-05) 27次浏览 已收录 0个评论

这篇文章主要介绍了Springboot2.0自适应效果错误响应过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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 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 { @Over<p style="color:transparent">来源gao!%daima.com搞$代*!码$网</p>ride public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map map = super.getErrorAttributes(webRequest, includeStackTrace); //获取自定义处理异常传递的参数 Map ext = (Map) 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自定义错误页面

   <title>4xx</title> <h1>status:[[${status}]]</h1><h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2>message:[[${message}]]</h2>

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自适应效果错误响应过程解析的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Springboot2.0自适应效果错误响应过程解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址