Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/seepine/wrap-spring-boot-starter
easy wrap for springboot
https://github.com/seepine/wrap-spring-boot-starter
spring-boot wrap
Last synced: about 3 hours ago
JSON representation
easy wrap for springboot
- Host: GitHub
- URL: https://github.com/seepine/wrap-spring-boot-starter
- Owner: seepine
- License: apache-2.0
- Created: 2021-11-28T08:22:41.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-15T07:02:55.000Z (over 1 year ago)
- Last Synced: 2024-11-19T05:55:20.144Z (about 17 hours ago)
- Topics: spring-boot, wrap
- Language: Java
- Homepage:
- Size: 141 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# spring-boot-starter-wrap
easy wrap for springboot
- 依赖引入后,所有接口返回都会被封装成{code:0,data:{},msg:''}的形式
- 当接口抛出WrapException,将会封装成{code:1,msg:''}
- 当接口不想被封装时,只需要在方法或类上加@NotWrap注解即可## 集成
### 引入依赖
- Latest
Version: [![Maven Central](https://img.shields.io/maven-central/v/com.seepine/wrap-spring-boot-starter.svg)](https://search.maven.org/search?q=g:com.seepine%20a:wrap-spring-boot-starter)
- Maven:```xml
com.seepine
wrap-spring-boot-starter
${Latest-Version}```
### 指定拦截包路径
不指定则默认拦截所有。若有集成Swagger,必须指定,否则可能导致`Unable to infer base url.`
```yaml
wrap:
#多个路径逗号隔开 com.example.controller1,com.example.controller2
scan-packages: com.example.controller
```## 例子
### 1 返回字符串类型
controller:
```java
class Controller {
@RequestMapping("hello")
public String hello() {
return "hello world";
}
}
```response:
`需要注意,个别请求库(例如flutter的dio)接收到的可能是jsonString,需要转为对象JSON.parse(jsonString)````json
{
"code": 0,
"data": "hello world"
}
```### 2 返回对象类型
entity:
```java
class User {
Long id;
String fullName;
Integer age;public User(Long id, String fullName, Integer age) {
this.id = id;
this.fullName = fullName;
this.age = age;
}
}
```controller:
```java
class Controller {
@RequestMapping("user")
public User user() {
return new User(1L, "jackson", 24);
}
}
```response:
```json
{
"code": 0,
"data": {
"id": 1,
"fullName": "jackson",
"age": 24
}
}
```### 3 带错误信息的异常处理
controller:
```java
class Controller {
@RequestMapping("sum")
public String sum() {
// ...
throw new WrapException("错误信息");
}
}
```response:
```json
{
"code": 1,
"msg": "错误信息"
}
```### 4 带数据的异常处理
controller:
```java
class Controller {
@RequestMapping("del")
public String del() {
//...
throw new WrapException(new Object(), "错误信息2");
}
}
```response:
```json
{
"code": -1,
"data": {
//..
},
"msg": "错误信息2"
}
```