So I want to achieve the functionality of saving an user
for that first i check whether user is present
if present throw an exception
else save the user
But when I throw an exception from service layer .flatMap(user -> Mono.error(new IllegalArgumentException("User Exists with email " + user.getEmail())))
@Service
@RequiredArgsConstructor
public class AppUserService {
private final AppUserRepository appUserRepository;
public Flux<AppUser> getAllUsers() {
return appUserRepository.findAll();
}
public Mono<AppUser> saveUser(AppUser appUser) {
return getUser(appUser.getEmail())
.flatMap(user -> Mono.error(new IllegalArgumentException("User Exists with email " + user.getEmail())))
.switchIfEmpty(Mono.defer(() -> appUserRepository.save(appUser))).cast(AppUser.class).log();
}
public Mono<AppUser> getUser(String email) {
return appUserRepository.findFirstByEmail(email);
}
}
and in the controller layer if I handle it like .onErrorResume(error -> ServerResponse.badRequest().bodyValue(error))
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class AppUserController {
private final AppUserService appUserService;
private final PasswordEncoder encoder;
@GetMapping
public Flux<AppUser> getAllUsers(@RequestHeader("email") String email) {
return appUserService.getAllUsers();
}
@PostMapping
@CrossOrigin
public Mono<ResponseEntity<Object>> saveUser(@RequestBody Mono<AppUser> appUserMono) {
return appUserMono
.doOnSuccess(appUser -> appUser.setPassword(encoder.encode(appUser.getPassword())))
.subscribeOn(Schedulers.parallel())
.flatMap(appUserService::saveUser)
.flatMap(savedAppUser -> ResponseEntity.created(URI.create("/user/" + savedAppUser.getId())).build())
.onErrorResume(error -> Response entity.badRequest().bodyValue(error))
.log();
}
}
it throws an error on the console
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.web.reactive.function.server.DefaultEntityResponseBuilder$DefaultEntityResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
and returns 200 to the client
what am i doing wrong
After reading the error it seems its getting an empty value
but if i debug the flow at the onErrorResume(error -> ..)
the error variable has the error
can't understand why it still throws the jackson error
is it because jackson can't subscribe to ServerResponse or something around that