java - Spring WebFlux: Emit exception upon null value in Spring Data MongoDB reactive repositories? -
i'm trying learn how use mongodb reactive repositories using spring-boot 2.0.0.m2
, fear i'm not doing things intended.
this 1 of methods, tries find user
email. if there none, method should throw exception.
@override public mono<user> findbyemail(string email) { user user = repository.findbyemail(email).block(); if(user == null) { throw new notfoundexception("no user account found email: " + email); } return mono.just(user); }
the repository extends reactivecrudrepository<user, string>
, fear using .block()
i'm preventing method being reactive. i'm new reactive programming, , i'm having hard time find documentation on it. can please point me in right direction?
reactive programming requires flow end-to-end reactive gain actual benefits come reactive programming model. calling .block()
within flow enforces synchronization , considered anti-pattern.
for code, propagate mono
obtain reactivecrudrepository
, apply switchifempty
operator emit error signal if mono
terminates without emitting value. null
values not allowed in reactive streams spec (the spec project reactor based on). if result yields no value, publisher
not emit value.
@override public mono<user> findbyemail(string email) { mono<user> fallback = mono.error(new notfoundexception("no user account found email: " + email)); return repository.findbyemail(email).switchifempty(fallback); }
see also:
Comments
Post a Comment