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
- Host: GitHub
- URL: https://github.com/zkdlu/api-response-spring-boot-starter
- Owner: zkdlu
- License: apache-2.0
- Created: 2021-03-16T11:56:01.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-04T16:40:01.000Z (over 2 years ago)
- Last Synced: 2023-03-04T23:08:37.327Z (over 2 years ago)
- Topics: exception-handler, library, restful, spring-boot
- Language: Java
- Homepage:
- Size: 86.9 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```