From 487183edb6c416da74787ed38be963ccb2377ad6 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Sun, 16 Aug 2020 14:20:08 +0200 Subject: [PATCH] Refactor sample application --- webfluxquestion/pom.xml | 65 +++++++++++----- .../java/com/webflux/QuestionApplication.java | 59 -------------- .../java/com/webflux/QuestionController.java | 77 +++++++++++++++++++ .../src/main/resources/application.properties | 2 +- webfluxquestion/webfluxquestion.iml | 2 - 5 files changed, 123 insertions(+), 82 deletions(-) create mode 100644 webfluxquestion/src/main/java/com/webflux/QuestionController.java delete mode 100644 webfluxquestion/webfluxquestion.iml diff --git a/webfluxquestion/pom.xml b/webfluxquestion/pom.xml index 0b8b280..d3e3cd6 100644 --- a/webfluxquestion/pom.xml +++ b/webfluxquestion/pom.xml @@ -1,26 +1,51 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.3.RELEASE + + - - org.springframework.boot - spring-boot-starter-parent - 2.3.2.RELEASE - - + webflux.question + webfluxquestion + 1.0-SNAPSHOT - webflux.question - webfluxquestion - 1.0-SNAPSHOT - - - - org.springframework.boot - - spring-boot-starter-webflux - - + + 11 + + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + io.projectreactor + reactor-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + \ No newline at end of file diff --git a/webfluxquestion/src/main/java/com/webflux/QuestionApplication.java b/webfluxquestion/src/main/java/com/webflux/QuestionApplication.java index dc59a99..1f5376f 100644 --- a/webfluxquestion/src/main/java/com/webflux/QuestionApplication.java +++ b/webfluxquestion/src/main/java/com/webflux/QuestionApplication.java @@ -1,17 +1,8 @@ package com.webflux; -import com.webflux.model.*; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; -@Controller @SpringBootApplication public class QuestionApplication { @@ -19,54 +10,4 @@ public static void main(String[] args) { SpringApplication.run(QuestionApplication.class); } - @PostMapping(path = "/question", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public Mono> refundCustomer(@RequestBody QuestionRequest questionRequest) { - Mono firstStepMono = getFirstStepObjectFromQuestionRequest(questionRequest); - - Mono secondStepMono = firstStepMono.flatMap(this::getSecondStepObjectFromFirstStepObject); - - Mono thirdStepMono = secondStepMono.flatMap(this::getThirdStepObjectFromSecondStepObject); - - Mono questionResponseMono = Mono.zip(firstStepMono, secondStepMono, thirdStepMono).flatMap(tuple -> getQuestionResponseFromAllThreeSteps(tuple.getT1(), tuple.getT2(), tuple.getT3())); - - return questionResponseMono.map(ResponseEntity::ok); - } - - private Mono getFirstStepObjectFromQuestionRequest(QuestionRequest questionRequest) { - String secretKey = getSecretKeyFromQuestionRequest(questionRequest); - return WebClient.create().post().uri("http://firstWebService:8111/getFirstStep") - .body(Mono.just(secretKey), String.class).retrieve().bodyToMono(FirstStep.class); - } - - private Mono getSecondStepObjectFromFirstStepObject(FirstStep firstStepObject) { - String something = getSomethingFromFirstStepObject(firstStepObject); - return WebClient.create().post().uri("http://secondWebService:8222/getSecondStep") - .body(Mono.just(something), String.class).retrieve().bodyToMono(SecondStep.class); - } - - private Mono getThirdStepObjectFromSecondStepObject(SecondStep secondStepObject) { - String somethingElse = getSomethingElseFromSecondStepObject(secondStepObject); - return WebClient.create().post().uri("http://thirdWebService:8333/getThirdStep") - .body(Mono.just(somethingElse), String.class).retrieve().bodyToMono(ThirdStep.class); - } - - private Mono getQuestionResponseFromAllThreeSteps(FirstStep firstStep, SecondStep secondStep, ThirdStep thirdStep) { - return Mono.just(new QuestionResponse(firstStep.getThingNeededInTheFinalResponse(), secondStep.getThingNeededInTheFinalResponse(), thirdStep.getThingNeededInTheFinalResponse())); - } - - private String getSecretKeyFromQuestionRequest(QuestionRequest questionRequest) { - System.out.println("I am a super heavy operation. I should be done only once. You should also see this line printed only once, But no..."); - return questionRequest.getThing().concat("dummy"); - } - - private String getSomethingFromFirstStepObject(FirstStep firstStepObject) { - System.out.println("I am another super heavy operation. I should be done only once. You should also see this line printed only once, But no..."); - return firstStepObject.getThingNeededInOrderToGetTheSecondStepObject().concat("anotherDummy"); - } - - private String getSomethingElseFromSecondStepObject(SecondStep secondStepObject) { - System.out.println("I am another other super heavy operation. I should be done only once. You should also see this line printed only once, But no..."); - return secondStepObject.getThingNeededInOrderToGetTheThirdStepObject().concat("anotherOtherDummy"); - } - } diff --git a/webfluxquestion/src/main/java/com/webflux/QuestionController.java b/webfluxquestion/src/main/java/com/webflux/QuestionController.java new file mode 100644 index 0000000..75f3a8b --- /dev/null +++ b/webfluxquestion/src/main/java/com/webflux/QuestionController.java @@ -0,0 +1,77 @@ +package com.webflux; + +import com.webflux.model.FirstStep; +import com.webflux.model.QuestionRequest; +import com.webflux.model.QuestionResponse; +import com.webflux.model.SecondStep; +import com.webflux.model.ThirdStep; +import reactor.core.publisher.Mono; +import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; + +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +@Controller +public class QuestionController { + + @PostMapping(path = "/question", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public Mono> refundCustomer(@RequestBody QuestionRequest questionRequest) { + return getFirstStepObjectFromQuestionRequest(questionRequest) + .flatMap(this::getSecondStepObjectFromFirstStepObject) + .flatMap((secondStep) -> getThirdStepObjectFromSecondStepObject(secondStep)) + .map(ResponseEntity::ok); + } + + private Mono> getFirstStepObjectFromQuestionRequest(QuestionRequest questionRequest) { + String secretKey = getSecretKeyFromQuestionRequest(questionRequest); + // webclient call to external service + Mono firstStep = Mono.just(new FirstStep("firstOne", "firstTwo")); + return firstStep.map((step) -> { + QuestionResponse response = new QuestionResponse(); + response.setThingFromTheFirstStep(step.getThingNeededInTheFinalResponse()); + return Tuples.of(step, response); + }); + } + + private Mono> getSecondStepObjectFromFirstStepObject(Tuple2 tuple) { + String something = getSomethingFromFirstStepObject(tuple.getT1()); + // webclient call to external service + Mono secondStep = Mono.just(new SecondStep("secondOne", "secondTwo")); + return secondStep.map((step) -> { + QuestionResponse response = tuple.getT2(); + response.setThingFromTheSecondStep(step.getThingNeededInTheFinalResponse()); + return Tuples.of(step, response); + }); + } + + private Mono getThirdStepObjectFromSecondStepObject(Tuple2 tuple) { + String somethingElse = getSomethingElseFromSecondStepObject(tuple.getT1()); + // webclient call to external service + Mono thirdStep = Mono.just(new ThirdStep("thirdOne")); + return thirdStep.map((step) -> { + QuestionResponse response = tuple.getT2(); + response.setThingFromTheThirdStep(step.getThingNeededInTheFinalResponse()); + return response; + }); + } + + private String getSecretKeyFromQuestionRequest(QuestionRequest questionRequest) { + System.out.println("I am a super heavy operation. I should be done only once. You should also see this line printed only once, But no..."); + return questionRequest.getThing().concat("dummy"); + } + + private String getSomethingFromFirstStepObject(FirstStep firstStepObject) { + System.out.println("I am another super heavy operation. I should be done only once. You should also see this line printed only once, But no..."); + return firstStepObject.getThingNeededInOrderToGetTheSecondStepObject().concat("anotherDummy"); + } + + private String getSomethingElseFromSecondStepObject(SecondStep secondStepObject) { + System.out.println("I am another other super heavy operation. I should be done only once. You should also see this line printed only once, But no..."); + return secondStepObject.getThingNeededInOrderToGetTheThirdStepObject().concat("anotherOtherDummy"); + } + +} diff --git a/webfluxquestion/src/main/resources/application.properties b/webfluxquestion/src/main/resources/application.properties index e6d8069..91b8ff1 100644 --- a/webfluxquestion/src/main/resources/application.properties +++ b/webfluxquestion/src/main/resources/application.properties @@ -1 +1 @@ -server.port=9999 \ No newline at end of file +#server.port=9999 \ No newline at end of file diff --git a/webfluxquestion/webfluxquestion.iml b/webfluxquestion/webfluxquestion.iml deleted file mode 100644 index 78b2cc5..0000000 --- a/webfluxquestion/webfluxquestion.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file