Springboot2.0中自适应错误响应解析流程如何调试?
- 内容介绍
- 文章标签
- 相关推荐
本文共计444个文字,预计阅读时间需要2分钟。
这篇文章主要介绍了Spring Boot 2.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.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"; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计444个文字,预计阅读时间需要2分钟。
这篇文章主要介绍了Spring Boot 2.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.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"; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

