@@ -13,7 +13,7 @@ flylib-boot是针对springboot构建的程序的基础框架,专门用于构
13
13
## 使用方法
14
14
- Step 1: 进入目录flylib-boot-starter,执行<code >mvn install</code >
15
15
- Step 2: 在自己的项目中添加flylib-boot-starter的maven依赖. 并留意自己使用的spring-boot版本,去修改自己的pom.xml文件
16
- ```
16
+ ``` xml
17
17
<dependency >
18
18
<groupId >org.flylib</groupId >
19
19
<artifactId >flylib-boot-starter</artifactId >
@@ -22,7 +22,7 @@ flylib-boot是针对springboot构建的程序的基础框架,专门用于构
22
22
```
23
23
并且要注意这里spring-boot版本是1.5.0.RELEASE. 另外需要添加spring-boot-maven-plugin
24
24
实例参考spring-boot-demo项目,它的pom如下:
25
- ```
25
+ ``` xml
26
26
<?xml version =" 1.0" encoding =" UTF-8" ?>
27
27
<project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
28
28
xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
@@ -76,7 +76,15 @@ flylib-boot是针对springboot构建的程序的基础框架,专门用于构
76
76
```
77
77
78
78
- Step 3: 在自己的程序中new 一个UserException(自定义的异常类)设置捕获异常
79
- ```
79
+ ``` java
80
+ /**
81
+ * 用户信息的异常
82
+ */
83
+ public class UserException extends RuntimeException {
84
+
85
+ }
86
+
87
+
80
88
@RequestMapping (" " )
81
89
public String index() throws RuntimeException {
82
90
UserException userException = new UserException ();
@@ -87,7 +95,7 @@ flylib-boot是针对springboot构建的程序的基础框架,专门用于构
87
95
```
88
96
- Step 4: 运行自己的Spring Boot项目
89
97
输出到浏览器的结果
90
- ```
98
+ ``` json
91
99
{
92
100
code:"001",
93
101
message:"User not exists",
@@ -97,7 +105,7 @@ flylib-boot是针对springboot构建的程序的基础框架,专门用于构
97
105
## 实现原理
98
106
利用了@ControllerAdvice 和@ExceptionHandler
99
107
实现代码是
100
- ```
108
+ ``` java
101
109
package org.flylib.boot.starter.handler ;
102
110
103
111
import org.flylib.boot.starter.exception.CustomRuntimeException ;
0 commit comments