Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/giuseppe998e/spring-webflux-response-wrapper
Spring WebFlux Response Wrapper (Telegram bot API style)
https://github.com/giuseppe998e/spring-webflux-response-wrapper
kotlin library response response-wrapper rest spring webflux wrapper
Last synced: about 2 months ago
JSON representation
Spring WebFlux Response Wrapper (Telegram bot API style)
- Host: GitHub
- URL: https://github.com/giuseppe998e/spring-webflux-response-wrapper
- Owner: giuseppe998e
- License: apache-2.0
- Created: 2022-04-07T13:31:28.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-03T14:18:52.000Z (over 2 years ago)
- Last Synced: 2024-05-02T04:09:05.049Z (9 months ago)
- Topics: kotlin, library, response, response-wrapper, rest, spring, webflux, wrapper
- Language: Kotlin
- Homepage:
- Size: 88.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring WebFlux Response Wrapper
This library allows you to wrap the content of REST responses in the format:
```json
{
"ok": true,
"data": "String/Number/Object/List"
}
```
...or, in case of an error:
```json
{
"ok": false,
"error": {
"value": 500,
"message": "..."
}
}
```> It will also replace not found paths with a 404 error response.
## Add dependency
### Maven
**1.** Add the JitPack repository to your _pom.xml_ file:
```xml
jitpack.io
https://jitpack.io
```
**2.** Add dependency:
```xmlcom.github.giuseppe998e
spring-webflux-response-wrapper
v0.2.4```
### Gradle
**1.** Add the JitPack repository to your _build.gradle_ file:
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
**2.** Add dependency:
```groovy
dependencies {
implementation 'com.github.giuseppe998e:spring-webflux-response-wrapper:v0.2.4'
}
```## Usage
### Kotlin
**1.** Import the configuration class:
```kotlin
package com.example.springimport brc.webflux.response.wrapper.ResponseWrapper
//...@SpringBootApplication
@Import(ResponseWrapper::class)
class ExampleMicroserviceApplicationfun main(args: Array) {
runApplication(*args)
}
```
**2.** Create a REST controller:
```kotlin
package com.example.springimport brc.webflux.response.wrapper.model.Response
// ...@Controller
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
class ExampleRestController {
/*
* Returns:
* ```
* {"ok":true,"data":"Example Response"}
* ```
*/
@GetMapping
fun defaultHandler(): Mono = Mono.just("Example Response")
/*
* Returns:
* ```
* {"ok":true,"data":["Example Response #1","Example Response #2","Example Response #3"]}
* ```
*/
@GetMapping("/flux")
fun fluxHandler(): Flux = Flux.fromArray(
arrayOf(
"Example Response #1",
"Example Response #2",
"Example Response #3"
)
)
/*
* Returns:
* ```
* {"ok":false,"error":{"value":500,"message":"Internal Server Error"}}
* ```
*/
@GetMapping("/error")
fun errorHandler(): Mono =
Mono.error(Exception("This error will be logged but not returned (Code: 500 - INTERNAL SERVER ERROR)"))
/*
* Returns:
* ```
* {"ok":false,"error":{"value":400,"message":"This error will NOT be logged, but returned"}}
* ```
*/
@GetMapping("/error/custom")
fun customErrorHandler(): Mono =
Mono.error(Response.Error(HttpStatus.BAD_REQUEST, "This error wont be logged but returned"))
}
```