英文:
Collecting int value from the Mono<Entity> object using WebFlux
问题
从Mono<Entity>对象中收集值的正确方法是什么?
我有一个名为Student的实体,它具有user和student id值。
Mono<Student> 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<Student> 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
。根据应用程序的预期行为,您可以使用defaultIfEmpty
或switchIfEmpty
操作符来处理该情况。 - 发生错误。您可以让错误通过管道流动,并由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<StudentSubject> studentSubject = studentRepository.findStudentByUserid(id)
.flatMap(student -> {
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 thatMono
from your controller method. - there is no student with that id, so the first repository will return
Mono.empty()
. In this case, yourflatMap
operator won't be called and an empty publisher is effectively returned; noStudentSubject
is created. Depending on the expected behavior of your app, you could look intodefaultIfEmpty
orswitchIfEmpty
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论