0% found this document useful (0 votes)
10 views2 pages

Reactive Programming

Uploaded by

mouneshgs96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Reactive Programming

Uploaded by

mouneshgs96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1) Synchronus based execution ( Blocking Thread )

2) Asynchronus based execution ( Non Blocking Thread )

=> Spring 5.x introduced Reactive Programming

=> In Spring 5.x 'starter-webflux' introduced

==================
Old Approach
==================
@RestController
public class WelcomeRestController{

@GetMapping("/msg")
public String getMsg(){
return "Hello";
}

===============
New Approach
===============

@Component
public class MessageRequestHandler{

public Mono<ServerResponse> handle(ServletRequest request){


return new ServerRespoinse.ok()
.contentType(MediaType.APPLICATION
_JSON)
.body(BodyInserters.fromValue(data
));
}

@Configuration
public MsgRouter {

@Bean
pubilc RouterFunction<ServerResponse> route(MessageRequestHandler
requestHandler){

return RouterFunctions.route(GET("/hello"))
.and(accept(MediaType.APPLIC
ATION_JSON), MessageRequestHandler::handle);

=============================
SprinBoot Reactive Example
============================

1) Create Boot application with 'Reactive Web' dependency

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

Note: Reactive Web dependency means 'starter-webflux' dependency. It will provide


'Netty' as default embedded container.

2) Create Binding class to response

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Greeting {

private String msg;

3) Create Request Handler class like below

@Component
public class GreetingHandler {

public Mono<ServerResponse> hello(ServerRequest request){

return ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(new Greeting("Hello
World")));
}
}

4) Create Router class

@Configuration
public class GreetingRouter {

@Bean
public RouterFunction<ServerResponse> route(GreetingHandler greeting){
return RouterFunctions
.route(GET("/hello")
.and(accept(MediaType.APPLICATION_JSON)), greeting::hello);
}
}

5) Run the application and test it.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy