3

I have the following route in my application

@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
    return RouterFunctions.route(RequestPredicates.POST("/users"), handler::signup);
}

which is handled by the following method

public Mono<ServerResponse> signup(ServerRequest request) {
    return request
            .bodyToMono(User.class)
            .map(user -> {
                user.setPassword(encoder.encode(user.getPassword()));
                return user;
            })
            .flatMap(repository::save)
            .map(user -> tokenService.create(user.getId()))
            .map(TokenDto::new)
            .flatMap(tokenDto -> ServerResponse.ok().body(tokenDto, TokenDto.class));
}

When I issue a valid request to the endpoint, the following error is logged

'producer' type is unknown to ReactiveAdapterRegistry

Having investigated the issue with a debugger, I see that my handler's signup method completes successfully, so I'm assuming the router doesn’t like the return value, but I'm not sure why?

  • 1
    When I use `ServerResponse.ok()`, I usually add `.contentType(MediaType.APPLICATION_JSON)` (or other content type) before `.body`. Are you allowed to do that in your case? – Felipe Mar 18 '21 at 07:24
  • Thanks I was missing that but unfortunately that hasn’t resolved the issue –  Mar 18 '21 at 12:39
  • 1
    I would try to separate the code by having the user created in another nested pipeline and then calling `return ServerResponse.ok().body(tokenDto, TokenDto.class);`. By the way, I don't understand how you create a new token based on the user if you don't pass the user at `.map(TokenDto::new)`. – Felipe Mar 18 '21 at 13:22
  • The new token is created based on the user's ID `user -> tokenService.create(user.getId())` then the returned JWS is used to create an instance of `TokenDto` which is subsequently returned to the client. –  Mar 18 '21 at 14:32

2 Answers2

4

I have the same problem with you, the same error. Finally I change the code using bodyValue method not body(T,Class),you can try this: ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).bodyValue(tokenDto)

bobxwang
  • 56
  • 2
  • Thanks, pal. Not working on this project anymore (this post is old,) but I appreciate you sharing a solution for others who face this problem. –  May 18 '22 at 18:45
  • Although I haven't tried your solution, I’ve accepted your answer, because having read the documentation for the method I was using and the method you suggested, I believe you're correct. –  May 18 '22 at 18:59
0

try to change tokenDto to Mono.just(tokenDto) it worked for me. I got the same problem just now.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 08 '22 at 04:31