webFlux 路由post请求时依赖注入字段报null

有下列代码:

@Autowired
AuthHandler authHandler;

RouterFunction<ServerResponse> doctorRouter =
         RouterFunctions.route()
                .path("/doctors", b1 -> b1
                        .nest(accept(MediaType.APPLICATION_JSON), b2 -> b2
                                .GET("/{id}", request -> ServerResponse.ok()
                                     .body(feignService.getDoctorById(request.headers().header("Authorization").get(0), Integer.valueOf(request.pathVariable("id"))),
                                                String.class)
                                )
                              .POST(authHandler::loginAuthentication)

                        ))
                .build();

当起动spring程序时报authHandler为空

但是当把代码修改为如下:

 RouterFunction<ServerResponse> doctorRouter(){
        return RouterFunctions.route()
                .path("/doctors", b1 -> b1
                        .nest(accept(MediaType.APPLICATION_JSON), b2 -> b2
                                .GET("/{id}", request -> ServerResponse.ok()
                                        .body(feignService
                                                .getDoctorById(request.headers().header("Authorization").get(0), Integer.valueOf(request.pathVariable("id"))),
                                                String.class)
                                )
                                .POST(authHandler::loginAuthentication)
                        ))
                .build();

   }

这里spring起动成功,有哪位大神知道原因?敬请留言。



来自为知笔记(Wiz)


posted on 2022-10-26 10:23  白衣风云  阅读(59)  评论(0编辑  收藏  举报

导航