0

I want to write a Rest API using reactive and achieve the below result. I've 2 microservices for this.

Service 1: Receives a post Object and retrieves some data from its database. The process something then communicate to the second Micro Service. Then return the Service 2 response as a Mono to the client.

Service 2: Receives the Data passed from Service 1 and save it to database and returns the Object with the generated primary key.

Here is my code look like.

@PostMapping("/submitData")
public Mono<Object> submitScore(@RequestBody SomeObject someObject){
    //Some Operations
    return contestGiftInfoRepository.findAllDataById(id)                
    .filter(c -> {
        
        if(condition) {
            return true;
        } else {
            return false;
        }
    }).reduce((item1,item2) -> {
        if(condition) {
            return item1;
        }else {
            return item2;
        }
    })
    .defaultIfEmpty(emptyObj)
    .map(resultObj->{
        //DoSome OPeration
        
        WebClient client = WebClient.create("my service Url 2");
        Mono<Obj> response = client.post().uri("/ExecutionPath")
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .body(Mono.just(resultObj), ResultObj.class)
        .retrieve()
        .bodyToMono(ResponseObj.class);

        return response;

    });
    
}

Now When I call the web service 1 everything works fine. When I call the second web service individually that works as well. However, when I call the second web service from the first service it returns a result reactor.core.Exceptions$ErrorCallbackNotImplemented: org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500 Internal Server Error from POST http://localhost:8085/path Caused by: org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500 Internal Server Error from POST http://localhost:8085/path at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:218) ~[spring-webflux-5.3.16.jar:5.3.16] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s):

I don't understand the actual issue from the error message. What I'm missing here?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Ram Kurup
  • 61
  • 1
  • 5
  • 2
    And what does the debug logs in the second service say? Have you looked at the raw requests and compared the one working with the request that doesnt? – Toerktumlare May 02 '22 at 13:17
  • 2
    In reactive nothing happened until you subscribe. You need to use `flatMap` instead of `map`. – Alex May 02 '22 at 14:47
  • @Toerktumlare Your question helped me. I was so silly and did not check the second service logs. When I checked I found a nested exception there. I was thinking in a way the exception will be logged into the first service console. Now resolved issues. Its working as expected. – Ram Kurup May 04 '22 at 06:01
  • @Alex It's working with map. However, I tried flatmap as well. Both work fine for my expected result. – Ram Kurup May 04 '22 at 06:02
  • 1
    Not sure how it could work. Based on the current code return type would be `Mono>` that matches `Mono` but not correct. Regarding "500 Internal Server Error", as @Toerktumlare suggested check the logs and provide more details. – Alex May 04 '22 at 14:38

0 Answers0