接口响应类型是html,全局异常处理区分返回响应类型是页面还是JSON

一、准备工作

1.1 导入依赖

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-thymeleaf

org.projectlombok

lombok

true

1.2 在 /templates 目录下新建 error 页面

统一页面异常处理

统一页面异常处理

1.3 定义异常基类

@Data

public class BaseException extends RuntimeException {

/**

* 错误码

*/

private Integer code;

/**

* 错误提示

*/

private String message;

public BaseException(Integer code, String message) {

this.code = code;

this.message = message;

}

}

二、根据 URL 后缀区分

2.1 创建 URL 异常类

@Data

public class UrlSuffixException extends BaseException {

public UrlSuffixException(Integer code, String message) {

super(code, message);

}

}

2.2 申明接口

@Controller

@RequestMapping("/error")

public class ExceptionController {

@RequestMapping("/url.html")

public void throwsUrlSuffixExceptionByHtml() {

throw new UrlSuffixException(0001, "throwUrlSuffixException");

}

@RequestMapping("/url.json")

public void throwsUrlSuffixExceptionByJson() {

throw new UrlSuffixException(0001, "throwUrlSuffixException");

}

}

2.3 全局异常处理 UrlSuffixException

@ControllerAdvice

public class GlobalExceptionHandle {

private static final String URL_SUFFIX_JSON = ".json";

@ExceptionHandler(UrlSuffixException.class)

public ModelAndView handleException(HttpServletRequest request, UrlSuffixException exception) {

String url = request.getRequestURL().toString();

if (url.endsWith(URL_SUFFIX_JSON)) {

ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

modelAndView.addObject("code", exception.getCode());

modelAndView.addObject("message", exception.getMessage());

return modelAndView;

} else {

ModelAndView modelAndView = new ModelAndView("/error");

modelAndView.addObject("message", exception.getMessage());

return modelAndView;

}

}

}

2.4 测试

访问http://localhost:8080/error/url.json

{

message: "throwUrlSuffixException",

code: 1001

}

访问 `http://localhost:8080/error/url.html

三、根据注解区分

3.1 创建注解异常类

@Data

public class AnnotationException extends BaseException {

public AnnotationException(Integer code, String message) {

super(code, message);

}

}

3.2 申明接口

@Controller

@RequestMapping("/error")

public class ExceptionController {

@RequestMapping("/annotation/html")

public void throwsAnnotationExceptionByHtml() {

throw new AnnotationException(2000, "throwAnnotationException");

}

@RequestMapping("/annotation/json")

@ResponseBody

public String throwsAnnotationExceptionByJson() {

throw new AnnotationException(2001, "throwAnnotationException");

}

}

3.3 全局异常处理

@ControllerAdvice

public class GlobalExceptionHandle {

@ExceptionHandler(AnnotationException.class)

public ModelAndView handleException(AnnotationException exception, HandlerMethod handler) {

if (isReturnJson(handler)) {

ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

modelAndView.addObject("code", exception.getCode());

modelAndView.addObject("message", exception.getMessage());

return modelAndView;

} else {

ModelAndView modelAndView = new ModelAndView("/error");

modelAndView.addObject("message", exception.getMessage());

return modelAndView;

}

}

private boolean isReturnJson(HandlerMethod handler) {

if (handler.getBeanType().isAnnotationPresent(RestController.class)) {

return true;

}

Method method = handler.getMethod();

if (method.isAnnotationPresent(ResponseBody.class)) {

return true;

}

return false;

}

}

3.4 测试

访问 http://localhost:8080/error/annotation/json

{

message: "throwAnnotationException",

code: 2001

}

访问 `http://localhost:8080/error/annotation/html

四、根据是否是 AJAX 请求区分

4.1 创建异常类

@Data

public class AjaxRequestException extends BaseException {

public AjaxRequestException(Integer code, String message) {

super(code, message);

}

}

4.2 申明接口

@Controller

@RequestMapping("/error")

public class ExceptionController {

@RequestMapping("/request")

@ResponseBody

public String throwsAjaxRequestException() {

throw new AjaxRequestException(3000, "throwAjaxRequestException");

}

}

4.3 全局异常处理

@ControllerAdvice

public class GlobalExceptionHandle {

@ExceptionHandler(AjaxRequestException.class)

public ModelAndView handleException(HttpServletRequest request, AjaxRequestException exception) {

if (isAjaxRequest(request)) {

ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

modelAndView.addObject("code", exception.getCode());

modelAndView.addObject("message", exception.getMessage());

return modelAndView;

} else {

ModelAndView modelAndView = new ModelAndView("/error");//配置需要跳转的Controller方法

modelAndView.addObject("message", exception.getMessage());

return modelAndView;

}

}

private boolean isAjaxRequest(HttpServletRequest request) {

String accept = request.getHeader("accept");

if (accept != null && accept.indexOf("application/json") != -1) {

return true;

}

String xRequestedWith = request.getHeader("X-Requested-With");

if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {

return true;

}

String uri = request.getRequestURI();

if (uri.endsWith(".json") || uri.endsWith(".xml")) {

return true;

}

String ajax = request.getParameter("__ajax");

if (ajax.endsWith("json") || ajax.endsWith("xml")) {

return true;

}

return false;

}

}

4.4 测试

访问http://localhost:8080/error/request

ajax 形式访问http://localhost:8080/error/request

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值