Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

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)

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:
```xml

com.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.spring

import brc.webflux.response.wrapper.ResponseWrapper
//...

@SpringBootApplication
@Import(ResponseWrapper::class)
class ExampleMicroserviceApplication

fun main(args: Array) {
runApplication(*args)
}
```
**2.** Create a REST controller:
```kotlin
package com.example.spring

import 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"))
}
```