rest-spring-boot-starter
Based on spring boot, unified business exception handling, packaging unified return form
rely
<dependency>
<groupId>tk.fishfish</groupId>
<artifactId>rest-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
Unified business Exception Handling
@RestController
public class DemoController {
@GetMapping("/list")
public List<String> list() {
throw new BizException(10001, "xxx错误");
}
}
Business exceptions unified treatment:
{
"code": 10001,
"msg": "xxx错误",
"data": null
}
Unified format packaging return
@RestController
public class DemoController {
@GetMapping("/list")
public List<String> list() {
return Arrays.asList("1", "2");
}
}
The return value packaging, ignore org.springframework.http.ResponseEntity
the value type.
{
"code": 0,
"msg": null,
"data": [
"1",
"2"
]
}
principle
Defined unified format:
{
"code": 返回码,
"msg": 描述,
"data": 数据
}
Conventions:
- code: -1, representative of the global error exception handling; 0, success; positive number, service error code
- msg: a description
- data: the actual data
Exception Handling
Use org.springframework.web.bind.annotation.RestControllerAdvice
the global catch the exception.
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);
}
}
Unified return
By implementing org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
, before writing a unified response returns the contents of the package.
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);
}
}