从Mono<Entity>对象中使用WebFlux收集整数值

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

Collecting int value from the Mono<Entity> object using WebFlux

问题

从Mono<Entity>对象中收集值的正确方法是什么?
我有一个名为Student的实体,它具有user和student id值。

Mono&lt;Student&gt; student = studentRepository.findStudentByUserid(id);

我想使用studentId(student.getStudentId())在不同的表中保存一个新值。

StudentSubject ss;
ss.setStudentId(student.getStudentId());
studentsubjectRepository.save(ss);

最佳方法是什么?

英文:

What is the correct way for collecting the value from the Mono<Entity> object?
I have an entity called Student which has both user and student id values.

Mono&lt;Student&gt; student = studentRepository.findStudentByUserid(id);

and I want to use studentId (student.getStudentId()) to save a new value in different table

StudentSubject ss;
ss.setStudentId(student.getStudentId());
studentsubjectRepository.save(ss);

What is the best approach?

答案1

得分: 2

你的问题缺少一些细节,所以我会从这个点开始做一些假设。

假设你的应用程序正在使用这段代码:

Mono<StudentSubject> studentSubject = studentRepository.findStudentByUserid(id)
    .flatMap(student -> {
        StudentSubject subject = new StudentSubject();
        subject.setStudentId(student.getStudentId());
        return studentsubjectRepository.save(subject);
    });

使用flatMap操作符可以以响应式方式链接调用。

这里可能发生几种情况:

  • 存在具有该id的学生,将使用该信息创建一个主题并返回,只要某个东西订阅了这个Mono。订阅部分通常在Spring WebFlux中发生,因此您只需要从控制器方法中返回那个Mono
  • 不存在具有该id的学生,因此第一个存储库将返回Mono.empty()。在这种情况下,您的flatMap操作符不会被调用,实际上会返回一个空的发布者;不会创建任何StudentSubject。根据应用程序的预期行为,您可以使用defaultIfEmptyswitchIfEmpty操作符来处理该情况。
  • 发生错误。您可以让错误通过管道流动,并由WebFlux处理,或者您可以使用onError*操作符直接处理错误。

如果您对Reactor不熟悉,您可以随时在链中的任何位置(作为常规操作符)添加log()操作符,您将在日志中精确地看到发生的情况。

英文:

Your question lacks a bit of details, so I'll assume some things from this point.

Let's say your app is using this code:

Mono&lt;StudentSubject&gt; studentSubject = studentRepository.findStudentByUserid(id)
    .flatMap(student -&gt; {
        StudentSubject subject = new StudentSubject();
        subject.setStudentId(student.getStudentId());
        return studentsubjectRepository.save(subject);
    });

Using the flatMap operator allows you to chain calls in reactive fashion.

Several things can happen here:

  • there is a student with that id, a subject is created with that information and is returned, as long as something subscribes to this Mono. The subscription part usually happens in Spring WebFlux so you just need to return that Mono from your controller method.
  • there is no student with that id, so the first repository will return Mono.empty(). In this case, your flatMap operator won't be called and an empty publisher is effectively returned; no StudentSubject is created. Depending on the expected behavior of your app, you could look into defaultIfEmpty or switchIfEmpty operators to deal with that case
  • there is an error. You can let the error flow throw the pipeline and being handled by WebFlux, or you can deal with it directly with onError* operators.

If you're new to Reactor, you can always add a log() operator wherever you want in the chain (as a regular operator) and you'll see in the logs what happens precisely.

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

发表评论

匿名网友

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

确定