返回Java对象从Mono

huangapple go评论105阅读模式
英文:

Returning Java Object from Mono

问题

我正在尝试从Mono<Object>获取Json字符串。我尝试使用block()方法获取对象,它运行正常,但是当我使用map/flatmap时,我看不到以下代码行被执行。我看到account Mono不是空的。

private String getJsonString(Mono&lt;Account&gt; account) {
    response.map(it-&gt;{
       **// 这里没有被调用**
        val json = mapper.writeValueAsString(it)
        System.out.println(son)
    });
}

我这里做错了什么吗?

英文:

I am trying to get Json String from Mono<Object>. I tried to use block() method to get object it worked fine , but when I use map/flatmap ,I don't see following lines of code is executed.And I see account Mono is not empty.

private String getJsonString( Mono&lt;Account&gt; account) {
        response.map(it-&gt;{
           **// call is not coming here** 
            val json = mapper.writeValueAsString(it)
            System.out.println(son)
        });
    }

am I doing anything wrong here?

答案1

得分: 4

If you give a read to the official documentation, you will see that:

>仅当您订阅时才会发生任何事情

Now to understand, In spring boot webflux based microservice, who is the subscriber?, have a look at this stackoverflow question

Now, if you think, you can have blocking and reactive implementations in the same service, unfortunately, it doesn't work like that. For this you have to understand the event loop model on which reactor works. Thus calling a block method at any point in the flow is of no good and is equivalent to using the old blocking spring-web methods. 因为处理请求的线程被阻塞并等待I/O操作/网络调用的结果。

Coming to your question in the comment:

>But when i use flatMap in my controller to call handler method it goes service method with Object not mono?serviceRequest-->Mono-->Object how this works?

Let me give you a simple example for this:

Suppose you have an employee application, where you want to fetch details of an employee for a given id.
Now in your controller, you will have an endpoint like this:

@GetMapping("/{tenant}/api/employee/{id}")
  public Mono<ResponseEntity> getEmployeeDetails(@PathVariable("id") Long employeeId) {
    return employeeService.getDetails(employeeId)
        .map(ResponseEntity::ok);
  }

Now in your service,

  public Mono<EmployeeEntity> getDetails(Long employeeId) {
    return employeeRepository.findById(employeeId);
  }

And your repository will look like this:

@Repository
public interface EmployeeRepository extends ReactiveCrudRepository<EmployeeEntity, Long> {

}
英文:

If you give a read to the official documentation , you will see that:

>Nothing happens until you subscribe

Now to understand, In spring boot webflux based microservice, who is the subscriber?, have a look at this stackoverflow question

Now, if you think, you can have blocking and reactive implementations in the same service, unfortunately, it doesn't work like that. For this you have to understand the event loop model on which reactor works. Thus calling a block method at any point in the flow is of no good and is equivalent to using the old blocking spring-web methods. Because the thread in which the request is being processed gets blocked and waits for the outcome of the I/O operation / network call.

Coming to your question in the comment:

>But when i use flatMap in my controller to call handler method it goes service method with Object not mono?serviceRequest-->Mono-->Object how this works?

Let me give you a simple example for this:

Suppose you have an employee application, where you want to fetch details of an employee for a given id.
Now in your controller, you will have an endpoint like this:

@GetMapping(&quot;/{tenant}/api/employee/{id}&quot;)
  public Mono&lt;ResponseEntity&gt; getEmployeeDetails(@PathVariable(&quot;id&quot;) Long employeeId) {
    return employeeService.getDetails(employeeId)
        .map(ResponseEntity::ok);
  }

Now in your service,

  public Mono&lt;EmployeeEntity&gt; getDetails(Long employeeId) {
    return employeeRepository.findById(employeeId);
  }

And your repository will look like this:

@Repository
public interface EmployeeRepository extends ReactiveCrudRepository&lt;EmployeeEntity, Long&gt; {

}

huangapple
  • 本文由 发表于 2020年7月31日 08:29:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63183243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定