Reputation: 74
I have these service methods which return Monos, and I'm trying to set the cookies with them. This doesn't work. Does anyone know how to do this?
I need this to return HttpResponse<Mono> and using block()
results in an error saying java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread default-nioEventLoopGroup-2-4
@Post
public Mono<HttpResponse<Mono<UserDto>>> login(@Body LoginDto loginDto, @Header(value = "User-Agent") String userAgentStr,
@CookieValue(value = "myapp-auth-deviceId") UUID deviceId) {
return registrationService.login(loginDto)
.flatMap(user -> {
MutableHttpResponse<Mono<UserDto>> httpResponse = HttpResponse.ok(Mono.just(user));
return registrationService.registerDevice(userAgentStr, deviceId, user)
.flatMap(device -> registrationService.getJwtToken(user, device))
.flatMap(jwtToken -> {
httpResponse.cookie(Cookie.of(DEVICE_ID_COOKIE_NAME, deviceId.toString()).path("/").maxAge(365 * 24 * 3600));
httpResponse.cookie(Cookie.of(AUTH_COOKIE_NAME, jwtToken).maxAge(365 * 24 * 3600));
return Mono.just((HttpResponse<Mono<UserDto>>) httpResponse);
});
}).single();
}
Upvotes: 1
Views: 42
Reputation: 74
Figured it out.
MutableHttpResponse<Mono<UserDto>> httpResponse = HttpResponse.ok();
UUID finalDeviceId = deviceId;
return httpResponse.body(registrationService.login(loginDto)
.flatMap(user -> registrationService.registerDevice(userAgentStr, finalDeviceId, user)
.flatMap(device -> registrationService.getJwtToken(user, device))
.map(jwtToken -> {
httpResponse.cookie(Cookie.of(DEVICE_ID_COOKIE_NAME, finalDeviceId.toString()).path("/").maxAge(365 * 24 * 3600));
httpResponse.cookie(Cookie.of(AUTH_COOKIE_NAME, jwtToken).path("/").maxAge(365 * 24 * 3600));
return user;
})));
This way it is waiting for the body, while I'm populating the cookies. The body is resolved after the cookies are populated.
Upvotes: 0
Reputation: 1
2.Set the cookie: -->Use the HttpResponse.cookie()method to create a respone with cookie.
Upvotes: 0