An open API service indexing awesome lists of open source software.

https://github.com/zkdlu/api-response-spring-boot-starter

Spring api response provides a standard structure to response data and exception handling
https://github.com/zkdlu/api-response-spring-boot-starter

exception-handler library restful spring-boot

Last synced: 2 months ago
JSON representation

Spring api response provides a standard structure to response data and exception handling

Awesome Lists containing this project

README

        

# Spring Api Response
The rest api classifies the HttpMethod and structured the url mapped to the resource. and have standard structure to response data.
Spring Api Response provides a standard structure to response data and exception handling

## Maven configuration
Add the Maven dependency:
```xml


jitpack.io
https://jitpack.io

com.github.zkdlu
api-response-spring-boot-starter
1.0.0

```

## Gradle configuration
Add the Gradle dependency:
```groovy
repositories {
maven { url 'https://jitpack.io' }
}

dependencies {
implementation 'com.github:zkdlu:api-response-spring-boot-starter:Tag'
}
```

## Getting Started
Here is a quick teaser in Spring boot Application:

```java
// Add Enable Annotation
@EnableResponse
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

// Defination Custom Exception
public class CustomException extends RuntimeException {
}

// application.yml
spring:
response:
success:
code: 200
msg: SUCCESS
exceptions:
custom1:
code: 400
msg: 'Something went wrong'
type: com.example.demo.CustomException
..

// Some Controller
public class DemoController {
@GetMapping("/")
public String test() {
return "demo"
}

@GetMapping("/model")
public MyModel test2() {
return MyModel.prototype();
}

@GetMapping("/exception")
public String test3() {
throw new CustomException();
}

@GetMapping("/list")
public List test4() {
return Array.asList(MyModel.prototype());
}
}
```

The response structure

- Before
```json
{
"msg": "hello"
}
```

- After
```json
# IF Sucess
{
"success": true,
"code": 200,
"msg": " | SUCCESS",
"data": {
"msg": "hello"
}
}

# IF Thrown Exception
{
"success": false,
"code": 400,
"msg": "",
"data":
}

```

## Build from Source
You don’t need to build from source (binaries in jitpack.io), but if you want to try out the latest, Use the built-in gradle wrapper. You don't need gradle, jdk, anything.

```bash
# macos
$ chomod +x ./gradlew
$ ./gradlew install
```