Spring Security构建Rest服务-0300-Restful API异常处理
SpringBoot默认的错误处理机制:
一、测试需要的部分代码
(完整代码放在了github https://github.com/lhy1234/spring-security):
UserController:只对新增用户做测试,省略其他代码
@RestController
@RequestMapping("/user")
public class UserController { /**
* 创建
* @Description:
* //@RequestBody:json映射到java
* @Valid 和User类上的@NotBlank注解一起做校验
* BindingResult存储的是校验错误信息
* @param @param user
* @param @return
* @return User
* @throws
* @author lihaoyang
* @date 2018年2月24日
*/
@PostMapping
public User create(@Valid @RequestBody User user){ //,BindingResult errors 先屏蔽BindingResult让springboot做错误处理,不屏蔽就能正常响应200了 // if(errors.hasErrors()){
// errors.getAllErrors().stream()
// .forEach(error -> System.err.println(error.getDefaultMessage()));
// } user.setId("1");
System.err.println(user);
return user;
} //省略其他代码。。。 }
User类:使用valid注解校验
public class User { public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView{}; //继承 private String id; @NotBlank(message="用户名不能为空")
private String username; @NotBlank(message="密码不能为空")
private String password;
@Past(message="生日只能是过去的日期")
private Date birthday; //省略get set }
一、对于进入controller之前就被打回的错误处理
1、对于url不存在的400错误处理
springboot对浏览器和app的请求,处理是不一样的:
1.1,使用浏览器,响应式的body一段html:
使用Springboot,启动服务,使用浏览器访问一个不存在的url后,出现html空白页:
浏览器发出的请求:请求头Content-Type:text/html;
1.2,使用chrome插件restlet client(https://restlet.com/modules/client/)模拟app访问:
使用restlet client模拟app访问不存在的url,响应体body是一个json:
图一
从上图可以看出,使用app发出的请求请求头是contentType是json类型
springboot错误处理类控制器 BasicErrorController代码片段:
这个控制器处理的url是 /error ,但是会根据请求头里类型的不同,做不同的处理,浏览器发的请求走上边的,返回时html;app发的请求,走下边的方法,返回是一个json。
这里提供了一种机制:同一个url,不同的情况做不同的处理。
上边是对url不存在的400错误的处理,从图一可以看出,springboot的响应信息很简单:
错误码 400,加上响应体body:
{
"timestamp": 1519608427470,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/xxx"
}
2,对于不符合校验规则的错误处理
User类的username和password是不能为空的,使用restlet client发出post请求,请求体为{},空的json,这样就不符合校验规则了(host是变量127.0.0.1):
响应头:
响应体:
会吧错误信息返回。
上边这两种情况,都是在进入controller之前就被spring打回的,一般我们需要在进入controller方法后,调用service过程中有异常来进行处理,下边模拟这种情况:
二、进入controller方法后发生了异常
在getInfo里直接抛出运行时异常模拟调用service发生错误:
/**
* 详情
* @Description: TODO
* @param @param id
* @param @return
* @return User
* @throws
* @author lihaoyang
* @date 2018年2月24日
*/
@GetMapping("{id:\\d+}") //{}里可以是正则,匹配数字
// @GetMapping("detail/{id}")
@JsonView(User.UserDetailView.class)
public User getInfo(@PathVariable(value="id",required=true) String id){ throw new RuntimeException("query deltail interface has error!"); // System.err.println(id);
// User user = new User();
// user.setUsername("tom");
// user.setPassword("123456");
// user.setId("1");
// return user;
}
使用浏览器访问:
对于浏览器也可以自定义错误页面,在src/main/resources下新建resources/error目录,可以自定义错误页面,这个对于app访问是无效的,app还是返回json
模拟app访问:错误中只包含抛出的异常信息
大部分情况,这种springboot对rest服务做的错误处理已经够用了。
如果还不够用也可以自己定义错误处理机制:
三、自定义异常
自定义异常:
package com.imooc.exception; /**
* 用户不存在异常
* ClassName: UserNotExistException
* @Description: TODO
* @author lihaoyang
* @date 2018年2月26日
*/
public class UserNotExistException extends RuntimeException{
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 1L; 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;
} }
controller方法:
@GetMapping("{id:\\d+}") //{}里可以是正则,匹配数字
// @GetMapping("detail/{id}")
@JsonView(User.UserDetailView.class)
public User getInfo(@PathVariable(value="id",required=true) String id){ // throw new RuntimeException("query deltail interface has error!");
throw new UserNotExistException(id); // System.err.println(id);
// User user = new User();
// user.setUsername("tom");
// user.setPassword("123456");
// user.setId("1");
// return user;
}
此时浏览器访问,返回自定义的错误页
区别就在于app访问,exception类已经是自定义的了,但是异常信息message还不够详细,下面介绍怎么处理
统一异常处理类:
package com.imooc.web.controller; import java.util.HashMap;
import java.util.Map; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import com.imooc.exception.UserNotExistException; /**
* @ControllerAdvice:处理其他controller抛出的异常,都会到这里处理
*/
@ControllerAdvice
public class ControllerExceptionHandler { @ExceptionHandler(UserNotExistException.class)//处理哪个异常类
@ResponseBody //返回json
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)//状态码 500
public Map<String,Object> handlerUserNotExistException(UserNotExistException ex){
Map<String,Object> result = new HashMap<>();
//通过ex对象拿到异常信息
result.put("id", ex.getId());
result.put("message", ex.getMessage());
return result;
}
}
app访问,可以打印自己想要的异常信息。
Spring Security构建Rest服务-0300-Restful API异常处理的更多相关文章
- Spring Security构建Rest服务-1001-spring social开发第三方登录之spring social基本原理
OAuth协议是一个授权协议,目的是让用户在不将服务提供商的用户名密码交给第三方应用的条件下,让第三方应用可以有权限访问用户存在服务提供商上的资源. 接着上一篇说的,在第三方应用获取到用户资源后,如果 ...
- Spring Security构建Rest服务-1300-Spring Security OAuth开发APP认证框架之JWT实现单点登录
基于JWT实现SSO 在淘宝( https://www.taobao.com )上点击登录,已经跳到了 https://login.taobao.com,这是又一个服务器.只要在淘宝登录了,就能直接访 ...
- Spring Security构建Rest服务-1202-Spring Security OAuth开发APP认证框架之重构3种登录方式
SpringSecurityOAuth核心源码解析 蓝色表示接口,绿色表示类 1,TokenEndpoint 整个入口点,相当于一个controller,不同的授权模式获取token的地址都是 /oa ...
- Spring Security构建Rest服务-1201-Spring Security OAuth开发APP认证框架之实现服务提供商
实现服务提供商,就是要实现认证服务器.资源服务器. 现在做的都是app的东西,所以在app项目写代码 认证服务器: 新建 ImoocAuthenticationServerConfig 类,@Ena ...
- Spring Security构建Rest服务-1200-SpringSecurity OAuth开发APP认证框架
基于服务器Session的认证方式: 前边说的用户名密码登录.短信登录.第三方登录,都是普通的登录,是基于服务器Session保存用户信息的登录方式.登录信息都是存在服务器的session(服务器的一 ...
- Spring Security构建Rest服务-0900-rememberMe记住我
Spring security记住我基本原理: 登录的时候,请求发送给过滤器UsernamePasswordAuthenticationFilter,当该过滤器认证成功后,会调用RememberMeS ...
- Spring Security构建Rest服务-0400-使用切片拦截rest服务
Restful API的拦截: 1,过滤器(Filter) 2,拦截器(Interceptor) 3,切片(Aspect) 1,过滤器 和传统javaweb一鸟样,例,记录controller执行时间 ...
- Spring Security构建Rest服务-1401-权限表达式
Spring Security 的权限表达式 用法,在自定义的BrowserSecurityConfig extends WebSecurityConfigurerAdapter 配置文件里,每一个a ...
- Spring Security构建Rest服务-1205-Spring Security OAuth开发APP认证框架之Token处理
token处理之二使用JWT替换默认的token JWT(Json Web Token) 特点: 1,自包含:jwt token包含有意义的信息 spring security oauth默认生成的t ...
- Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理
token处理之一基本参数配置 处理token时间.存储策略,客户端配置等 以前的都是spring security oauth默认的token生成策略,token默认在org.springframe ...
随机推荐
- I2C总线驱动框架详解
一.I2C子系统总体架构 1.三大组成部分 (1)I2C核心(i2c-core):I2C核心提供了I2C总线驱动(适配器)和设备驱动的注册.注销方法,I2C通信方法(”algorithm”)上层的,与 ...
- The serializable class XXX does not declare a static final serialVersionUID field of type long
问题: 在Eclipse中,继承类时,总是提示下面的警告(Warning),按理说警告是没有关系的,但是程序看起来老不爽了,这是强迫症的关系(呵呵) The serializable class XX ...
- WriteableBitmap(一)
通常,WPF中的位图是不可变的.不可变的位图非常有效,如果您希望进行大量的动态更改,创建和销毁它们的开销会变得非常昂贵.在这种情况下,您需要一些更灵活的东西——WriteableBitmap. Wri ...
- OpenNI体感应用开发实战 (Kinect相机)
一直以为Kinect是双目摄像机,或者是三目,看到那三个摄像头怎么可能不产生这样的疑惑,实际上它确实是单目摄像机,它三个摄像头分别为:红外发射器,RGB彩色相机,红外接收器.而其中,红外发射器和红外接 ...
- OpenGL常用的函数
OpenGL常用的函数 1. void glBegin(GLenummode) void glEnd(void) 参数说明: mode:创建图元的类型.可以是以下数值 GL_POINTS:把每一个顶点 ...
- Two ways to see predicates added by VPD or FGAC
http://www.bobbydurrettdba.com/2012/07/17/two-ways-to-see-predicates-added-by-vpd-or-fgac/ Two ways ...
- 更改GeoServer的端口号
更改GeoServer的端口号,这一问题在不同的GeoServer版本上的解决办法不禁相同.本文记录GeoServer2.7.6(独立安装)版本更改其端口号的办法. GeoServer默认端口为808 ...
- DevOps Workshop 研发运维一体化(广州站)
第一天对软件开发的需求管理.项目计划和源代码管理进行的全面而深入的介绍,并且为到会的所有开发人员提供现场动手实验的机会,大家兴致高涨,按照我们的操作手册完成了所有实验课题. 第二天主要介绍了最新的自动 ...
- CSS选择器分类总结
一.选择器语法及其意义(pattern and meaning) Pattern Meaning CSS level E an element of type E 标记选择器,匹配所有标签名为E的元素 ...
- WebAPI开发中的定时处理
https://blog.csdn.net/lordwish/article/details/77931897