rest-spring-boot-starter
rest-spring-boot-starter
基于spring boot,统一业务异常处理,统一返回格式包装
依赖
<dependency>
<groupId>tk.fishfish</groupId>
<artifactId>rest-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
统一业务异常处理
@RestController
public class DemoController {
@GetMapping("/list")
public List<String> list() {
throw new BizException(10001, "xxx错误");
}
}
业务异常统一处理:
{
"code": 10001,
"msg": "xxx错误",
"data": null
}
统一返回格式包装
@RestController
public class DemoController {
@GetMapping("/list")
public List<String> list() {
return Arrays.asList("1", "2");
}
}
返回值进行包装,忽略org.springframework.http.ResponseEntity值类型。
{
"code": 0,
"msg": null,
"data": [
"1",
"2"
]
}
原理
定义统一格式:
{
"code": 返回码,
"msg": 描述,
"data": 数据
}
约定:
- code:-1,代表全局异常处理的错误;0,成功;正数,业务错误码
- msg:一段描述
- data:真正的数据
异常处理
使用org.springframework.web.bind.annotation.RestControllerAdvice,全局捕获异常。
package tk.fishfish.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常处理,优先级为100
*
* @author 奔波儿灞
* @since 1.0
*/
@Order(100)
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(BizException.class)
public ApiResult<Void> handleBizException(BizException e) {
LOG.warn("handle bizException", e);
Integer code = e.getCode();
String msg = e.getMessage();
return ApiResult.fail(code, msg);
}
@ExceptionHandler(Exception.class)
public ApiResult<Void> handleException(Exception e) {
LOG.warn("handle exception", e);
String msg = e.getMessage();
return ApiResult.fail(-1, msg);
}
}
统一返回
通过实现org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice,在写入响应前统一包装返回内容。
package tk.fishfish.rest;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* 返回body处理,包装成ApiResult。如果是ResponseEntity,则不包装
*
* @author 奔波儿灞
* @see tk.fishfish.rest.ApiResult
* @since 1.0
*/
@RestControllerAdvice(annotations = RestController.class)
public class ApiResultResponseBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter methodParameter,
MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass,
ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
// ResponseEntity,则不包装
if (body instanceof ResponseEntity) {
return body;
}
// ApiResult,也不包装
if (body instanceof ApiResult) {
return body;
}
// todo 基本数据类型是否特殊处理
// 其余统一包装成ApiResult
return ApiResult.ok(body);
}
}
项目地址
rest-spring-boot-starter的更多相关文章
- Spring Boot Starter 介绍
http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...
- spring -boot s-tarter 详解
Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- SpringBoot 之Spring Boot Starter依赖包及作用
Spring Boot 之Spring Boot Starter依赖包及作用 spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. spri ...
- Spring boot starter pom的依赖关系说明
Spring Boot 通过starter依赖为项目的依赖管理提供帮助.starter依赖起始就是特殊的maven依赖,利用了传递依赖解析,把常用库聚合在一起,组成了几个为特定功能而定制的依赖. sp ...
- Spring Boot Starter列表
转自:http://blog.sina.com.cn/s/blog_798f713f0102wiy5.html Spring Boot Starter 基本的一共有43种,具体如下: 1)spring ...
- 创建自己的Spring Boot Starter
抽取通用模块作为项目的一个spring boot starter.可参照mybatis的写法. IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入sp ...
- 自己写spring boot starter
自己写spring boot starter 学习了:<spring boot实战>汪云飞著 6.5.4节 pom.xml <project xmlns="http://m ...
- 自定义的Spring Boot starter如何设置自动配置注解
本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...
- 手把手教你定制标准Spring Boot starter,真的很清晰
写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...
随机推荐
- 前端性能优化 http请求的过程及潜在的优化点
CS架构:比如我们的代码开发好,打包成apk,发布到平台,那么最终怎么运行到用户的手机上呢,用户首先需要从相关的应用商城下载这个apk包,并且运行这个 apk 包,那么这个 apk 包就会被解压,最后 ...
- pandas 生成并排放置的条形图和箱线图
1.代码 import numpy as np import pandas as pd import matplotlib.pyplot as plt # 生成数据,创建 DataFrame np.r ...
- Word 2016中公式不能自动斜体的解决方法
参考资料: 中文版 Office 数学公式默认不是斜体 为什么Word 2007中的公式编辑器字体不能自动倾斜 归纳总结 这个问题自Word 2007开始就存在,直至我目前用的Word 2016都没有 ...
- Java多线程编程核心技术-第5章-定时器 Timer-读书笔记
第 5 章 定时器 Timer 定时 / 计划功能在移动开发领域使用较多,比如 Android 技术.定时计划任务功能在 Java 中主要使用的就是 Timer 对象,他在内部使用多线程的方式进行处理 ...
- nginx 跨域请求访问
1.nginx跨域请求访问 location ~ .*\.(htm|html)$ { add_header Access-Control-Allow-Origin(请求域名) *(所有域名) http ...
- ActiveMQ消息可靠性-持久性
三个方面保证消息的可靠性 1.消息的持久 2.事物 3.签收 一:PERSISTENT:持久性 参数说明:1.持久 2.非持久 Java里面设置持久化和非持久 持久: 将持久性设置为持久 宕机 ...
- 费马平方和定理&&斐波那契恒等式&&欧拉四平方和恒等式&&拉格朗日四平方和定理
费马平方和定理 费马平方和定理的表述是:奇素数能表示为两个平方数之和的充分必要条件是该素数被4除余1. 1. 如果两个整数都能表示为两个平方数之和的形式,则他们的积也能表示为两个平方数之和的形式. $ ...
- learning shell check requires root privileges
[Purpose] Shell script check requires root privileges [Eevironment] Ubuntu 16.04 bas ...
- Linux下网络socket编程——实现服务器(select)与多个客户端通信
一.关于socket通信 服务器端工作流程: 调用 socket() 函数创建套接字 用 bind() 函数将创建的套接字与服务端IP地址绑定 调用listen()函数监听socket() 函数创建的 ...
- 【JZOJ5738】【20190706】锁屏杀
题目 $n \le 2000 $ 题解 \(B\)的数字可以对1440取模,对三个图分别进行\(dp\)即可 时间复杂度\(O(n\times 1440 \times 10)\) Code #incl ...