mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
4.4.0
This commit is contained in:
@@ -79,6 +79,56 @@ public class OpenServiceConfig extends AlipayServiceConfiguration {
|
||||
}
|
||||
```
|
||||
|
||||
- 全局异常处理
|
||||
|
||||
在微服务项目的全局异常处理中添加一句:`ExceptionHolder.hold(request, response, exception);`
|
||||
|
||||
```java
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody
|
||||
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
||||
...
|
||||
// 在返回前加这一句
|
||||
ExceptionHolder.hold(request, response, exception);
|
||||
...
|
||||
return ..;
|
||||
}
|
||||
```
|
||||
|
||||
如果没有配置全局异常,可参考下面配置
|
||||
|
||||
```java
|
||||
@ControllerAdvice
|
||||
@Slf4j
|
||||
public class StoryGlobalExceptionHandler {
|
||||
|
||||
|
||||
/**
|
||||
* 捕获手动抛出的异常
|
||||
*
|
||||
* @param request request
|
||||
* @param response response
|
||||
* @param exception 异常信息
|
||||
* @return 返回提示信息
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody
|
||||
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
||||
// 在返回前加这一句
|
||||
ExceptionHolder.hold(request, response, exception);
|
||||
// 下面可以实现自己的全局异常处理
|
||||
return new ErrorResult(500, exception.getMessage());
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class ErrorResult {
|
||||
private int code;
|
||||
private String msg;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
到此准备工作就完成了,接下来可前往`新增接口`查看如何新增接口。
|
||||
|
||||
## 非Java项目接入
|
||||
|
Reference in New Issue
Block a user