【Spring Security OAuth2笔记系列】- 【使用Spring MVC开发RESTful API】 服务异常处理

服务异常处理

本节内容,RESTful API错误处理

  • spring boot 默认的处理机制
  • 自定义异常处理

场景演示

用post请求的时候(可以用postman或则视频中讲解的谷歌插件 Restlet Client - REST API Testing );
Restlet Client - REST API Testing 这个谷歌插件可以去了解下,感觉挺好用的,可以项目分类和全部执行测试

Restlet Client 中请求后,可以点击返回的状态码,会跳转到规范中去,还挺不错:https://tools.ietf.org/html/rfc7231#section-6.5.4 比如404状态

通过浏览器和客户端(postman等工具)分别访问一个不存在的地址:比如 http://localhost:8080/xxx

浏览器:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Aug 02 13:40:20 GMT+08:00 2018
There was an unexpected error (type=Not Found, status=404).
No message available

客户端:
{
    "timestamp": "2018-08-02 13:42:56",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/xxx"
}

会发现同一个地址,不同的请求头访问会返回不同的结果;那么这种是怎么实现的呢?

有一个类是处理这种情况的;(具体是怎么发现的,我也不知道)

org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {

@RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request,
    HttpServletResponse response) {

@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {      

RequestMapping 居然还能这么使用,没有看明白;但是需要知道的是:

  • 可以通过@RequestMapping(produces = "text/html")来接受指定的Content-Type访问
  • 没有任何指定的将接受所有的请求头,但是优先匹配指定的路径

刚才访问的路径请求头(request header里面)来对比下

浏览器:Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

客户端:Accept: */*

一定要注意一个思想:

restfull api 是通过http状态码返回不同的场景,所以这里status是404.而且请求响应状态也是404;自己在设计的时候可以参考这个思想;
还有一个提示信息 “No message available” 404 状态就是没有找到可处理的路径,没有可用的信息也符合解释

但是在实际开发中,只是凭借http状态码的话,是不能解决实际场景中的需求的;
比如 创建用户信息,缺少字段,需要返回哪一个字段缺少?

在spring 默认的处理中,使用 @Valid 但是不声明BindingResult的时候,会把很详细的验证信息返回去;

自定义异常处理

在浏览器中发出的错误信息定义

在资源文件夹下创建对应状态码的html页面就可以了
|- resources
  |- error
    |- 404.html
    |- 500.html
但是在本版本中,存储的定义之后,并没有什么效果;把error放在templates中也没有效果;以后再研究了吧

补充:默认的html文件只能放在 static目录下才有效果

有自定义逻辑的异常处理

  • 自定义一种异常
  • @ControllerAdvice注解标识一个异常处理器

自定义用户不存在异常

package com.example.demo.exception;

/**
 * ${desc}
 * @author zhuqiang
 * @version 1.0.1 2018/8/2 14:20
 * @date 2018/8/2 14:20
 * @since 1.0
 */
public class UserNotExistException extends RuntimeException {
    private String id;

    public UserNotExistException(String id) {
        super("user not exist");
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

增加异常处理器

package com.example.demo.web.controller;

import com.example.demo.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ${desc}
 * @author zhuqiang
 * @version 1.0.1 2018/8/2 14:26
 * @date 2018/8/2 14:26
 * @since 1.0
 */
@ControllerAdvice
public class ControllerExceptionHandler {
    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody  // 用json方式返回
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)   // 返回http状态码
    public Map<String, Object> handleUserNotExistException(UserNotExistException ux) {
        Map<String, Object> resullt = new HashMap<>();
        resullt.put("id", ux.getId());
        resullt.put("message", ux.getMessage());
        return resullt;
    }
}

@ExceptionHandler中有一个异常对象,也就是为什么可以在自定义方式入参中能拿到异常对象的原因

猜你喜欢

转载自blog.csdn.net/mr_zhuqiang/article/details/81666711