Skip to content

Spring WebFlux Response Wrapper (Telegram bot API style)

License

Notifications You must be signed in to change notification settings

giuseppe998e/spring-webflux-response-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring WebFlux Response Wrapper

This library allows you to wrap the content of REST responses in the format:

{
    "ok": true,
    "data": "String/Number/Object/List"
}

...or, in case of an error:

{
    "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:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

2. Add dependency:

<dependency>
    <groupId>com.github.giuseppe998e</groupId>
    <artifactId>spring-webflux-response-wrapper</artifactId>
    <version>v0.2.4</version>
</dependency>

Gradle

1. Add the JitPack repository to your build.gradle file:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

2. Add dependency:

dependencies {
    implementation 'com.github.giuseppe998e:spring-webflux-response-wrapper:v0.2.4'
}

Usage

Kotlin

1. Import the configuration class:

package com.example.spring

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

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

fun main(args: Array<String>) {
    runApplication<ExampleMicroserviceApplication>(*args)
}

2. Create a REST controller:

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<String> = Mono.just("Example Response")
    
    /*
     * Returns:
     * ```
     * {"ok":true,"data":["Example Response #1","Example Response #2","Example Response #3"]}
     * ```
     */
    @GetMapping("/flux")
    fun fluxHandler(): Flux<String> = 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<String> =
        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<String> =
        Mono.error(Response.Error(HttpStatus.BAD_REQUEST, "This error wont be logged but returned"))
}