- Building RESTful Web Services with Spring 5(Second Edition)
- Raja CSP Raman Ludovic Dewailly
- 367字
- 2021-06-30 19:13:29
Reactive server initialization
Before jumping in to the endpoint, we will explore the structure of our files, including the initializer, handler, and repository.
The Server class for initializing our port 8081 is as follows:
public class Server {
public static final String HOST = "localhost";
public static final int PORT = 8081;
public static void main(String[] args) throws InterruptedException, IOException{
Server server = new Server();
server.startReactorServer();
System.out.println("Press ENTER to exit.");
System.in.read();
}
public void startReactorServer() throws InterruptedException {
RouterFunction<ServerResponse> route = routingFunction();
HttpHandler httpHandler = toHttpHandler(route);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create(HOST, PORT);
server.newHandler(adapter).block();
}
public RouterFunction<ServerResponse> routingFunction() {
// our Endpoints will be coming here
}
}
In the preceding method, we created a main class. Inside the main method, we will initialize the server and start the server with the following code:
Server server = new Server();
server.startReactorServer();
The preceding method will start the Reactor server. The Reactor server implementation is as follows:
RouterFunction<ServerResponse> route = routingFunction();
HttpHandler httpHandler = toHttpHandler(route);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create(HOST, PORT);
server.newHandler(adapter).block();
Let's go through this code later, as the concept is Reactive-based. Let's assume that this code works fine and we will move on, focusing on the endpoints.
The following is the method for mapping all REST endpoints for our CRUD operations:
public RouterFunction<ServerResponse> routingFunction() {
// our Endpoints will be coming here
}
You might get errors on UserRepository and UserHandler. Let's fill these up now:
package com.packtpub.reactive;
public interface UserRepository {
// repository functions will be coming here
}
In the preceding code, we have just added the UserRepository interface in our existing package com.packtpub.reactive. Later, we will introduce abstract methods for our business requirements.
Now, we can add a UserHandler class, and add the necessary things:
package com.packtpub.reactive;
// import statements
public class UserHandler {
private final UserRepository userRepository;
public UserHandler(UserRepository userRepository){
this.userRepository = userRepository;
}
}
In the preceding code, the UserHandler initializes the UserRepository instance in its constructor. If someone gets an instance of UserHandler, they will have to pass the UserRepository type to the UserHandler constructor. By doing this, UserRepository will always be forwarded to UserHandler to fulfill the business requirements.
- Mastering Node.js(Second Edition)
- 網(wǎng)絡(luò)云百問百答
- 面向物聯(lián)網(wǎng)的CC2530與傳感器應(yīng)用開發(fā)
- Building E-commerce Sites with VirtueMart Cookbook
- Hands-On Chatbots and Conversational UI Development
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- JBoss EAP6 High Availability
- 信息通信網(wǎng)絡(luò)建設(shè)安全管理概要2
- Go Web Scraping Quick Start Guide
- 企業(yè)私有云建設(shè)指南
- 電力物聯(lián)網(wǎng)工程技術(shù)原理與應(yīng)用
- 雷達(dá)饋線技術(shù)
- Yii Application Development Cookbook(Second Edition)
- 中國互聯(lián)網(wǎng)發(fā)展報告2018
- 圖解手機(jī)元器件維修技巧