Kotlin Spring WebFlux Annotation-Based RestAPIs – with SpringBoot 2

Reactive programming is about non-blocking applications. And Spring Framework 5 includes a new spring-webflux module, supports Reactive Streams for communicating backpressure across async components and libraries. So in the tutorial, JavaSampleApproach will guide you through the steps for creating a Kotlin Spring WebFlux Annotation-Based RestAPIs with SpringBoot 2.

Related posts:
Kotlin Spring MVC RequestMapping RESTful APIs with @GetMapping, @PostMapping, @PutMapping, @DeleteMapping | SpringBoot Example

I. Technologies

– Java: 1.8
– Maven: 3.5.2
– Spring Tool Suite: Version 3.9.0.RELEASE
– Spring Boot: 2.0.0.M7
– Kotlin 1.1.61
– Spring Boot Starter Webflux
– Postman client

II. Spring WebFlux

Spring Framework 5.0 supports WebFlux with fully asynchronous and non-blocking and does NOT require the Servlet API(Unlike Spring MVC).

Spring WebFlux supports 2 distinct programming models:
– Annotation-based with @RestController
– Functional with Java 8 lambda style

In the tutorial, we will introduce WebFlux with Annotation-based and Kotlin language.
For starting with WebFlux, SpringBoot supports a collection dependency: spring-boot-starter-webflux.

Kotlin sample code:

reactor.core.publisher.Flux: is a standard Publisher representing a reactive sequence of 0..N items, optionally terminated by either a success signal or an error.
reactor.core.publisher.Mono: Mono is a specialized Publisher that emits at most single-valued-or-empty result.

III. Practice

In the tutorial, We create a Kotlin SpringBoot 2 project as below:

Kotlin Spring WebFlux AnnotationBased RestAPIs - project structure

Step to do:
– Create Kotlin SpringBoot project
– Create data models
– Implement Spring WebFlux APIs
– Run and check results

1. Create Kotlin SpringBoot project

Using SpringToolSuite, create a Kotlin SpringBoot 2 project with Reactive Web dependency spring-boot-starter-webflux:

2. Create data models

– Create Address data model:

– Create Customer data model:

3. Implement Spring WebFlux APIs

4. Run and check results

Run the Kotlin SpringBoot project with commandline: mvn spring-boot:run

-> See console’ logs:

– Make a GET all customer request: http://localhost:8080/api/customer

Kotlin Spring WebFlux AnnotationBased RestAPIs - get all customers

– Make a GET customer request: http://localhost:8080/api/customer/1

Kotlin Spring WebFlux AnnotationBased RestAPIs - get a customer

– Make a POST request: http://localhost:8080/api/customer/post

Kotlin Spring WebFlux AnnotationBased RestAPIs - post a customer

– Make a PUT request: http://localhost:8080/api/customer/put/3

Kotlin Spring WebFlux AnnotationBased RestAPIs - put a customer

– Make a DELETE request: http://localhost:8080/api/customer/delete/1

Kotlin Spring WebFlux AnnotationBased RestAPIs - delete a customer

– Make a GET all customers request again: http://localhost:8080/api/customer

Kotlin Spring WebFlux AnnotationBased RestAPIs - get all customers

-> Logs from Kotlin SpringBoot server:

IV. Sourcecode

SpringBootKotlinWebFluxAnnotationBased

By grokonez | December 10, 2017.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*