I am new Spring Webflux. I am writing a simple api which call another api and returns response. The problem I have is my api takes diffrent type of request than the external api.I have to convert the incoming request to send to external api.I am using Mono to receive request for my api, but having trouble to convert to another object without block().
Input
Router
@Configuration
@EnableWebFlux
public class RouterConfig implements WebFluxConfigurer{
@Bean
public RouterFunction<ServerResponse> routes(UserHandler handler){
return RouterFunctions
.route(POST("/rest/create"),
handler::createUser);
}
}
Handler
@Component
public class UserHandler {
private UserService service;
public UserHandler(UserService service) {
this.service = service;
}
public Mono<ServerResponse> saveUser(ServerRequest request)
{
Mono<User> user = request.bodyToMono(User.class)
/* currently I am using block to get User object */
User user1 = user.block()
/* convert user to person */
Person p =getPersonFromUser(user);
}
Pojos
class User
{
private String name;
private String id;
private String email;
private String phone;
}
class Person
{
private String email;
/* Combination of id and name */
private String accountNumber;
private String phone;
}
Is there a way I can convert the Mono to Person object without blocking?