• 插件
  • 为何采用Functional Endpoint方式定义API就不行,换成Annotated Controller就可以?

如题:
halo版本:2.13

定义api:

@Component
@AllArgsConstructor
public class TodoListRouterFactory {
/**
* @return a {@link RouterFunction} instance
*/
@Bean
RouterFunction<ServerResponse> todoListRouter() {
RouterFunction<ServerResponse> route = route()
.GET("/hello-world", accept(MediaType.TEXT_PLAIN),
request -> ServerResponse.ok().bodyValue("Hello World")).build();
return route;
}
}
以上定义的api不起作用。

用以下方式就ok
@ApiVersion("fake.halo.run/v1alpha1")
@RequestMapping("/apples")
@RestController
public class AppleController {

@PostMapping("/starting")
public void starting() {
}

}

哪位高手指点一下?特别感谢!

    sophic 你说的不起作用是什么表现呢 访问localhost:8090/hello-world 是404的意思吗

      sophic 直接在浏览器访问 http://localhost:8090/hello-world 你写的 Functional Endpoint 方式是没有问题的可以得到 hello world 的文字,所以我不知道你说的没有效果是什么意思,如果 swagger 不行那就试试 postman 之类的

      @Component
      @AllArgsConstructor
      public class TodoListRouterFactory {
      
          @Bean
          RouterFunction<ServerResponse> todoListRouter() {
              return route()
                  .GET("/hello-world", accept(MediaType.TEXT_PLAIN),
                      request -> ServerResponse.ok().bodyValue("Hello World")).build();
          }
      }

        guqing 确实OK了,我以为在swagger里也应该有的,不知为何swagger看不见。

        貌似没有访问控制,Annotated Controller就自然有访问控制了,需要登录才可以。

          sophic Controller 是因为限定了必须要注解 @ApiVersion 所以会自动添加前缀 /apis,Halo 文档中说明以 /api 或 /apis 开头的 API 需要认证或鉴权后才可访问,至于需要在 swagger 中看得到可以参考 spring-doc-openapi 的文档:
          https://springdoc.org/#spring-webfluxwebmvc-fn-with-functional-endpoints
          这是其中提供的示例:springdoc/springdoc-openapi-demosblob/2.x/demo-spring-boot-3-webflux-functional/src/main/java/org/springdoc/demo/app4/employee/EmployeeFunctionalConfig.java
          或者参考 Halo 的示例:halo-dev/haloblob/6ea7abeff2f2a4aaa8d24d0385fc4618e7db7844/application/src/main/java/run/halo/app/core/extension/endpoint/PostEndpoint.java#L56-L66
          区别在于原先通过 RouterFunctions.route() 构建的 RouterFunction 是不带文档的在 swagger 上看不到,而通过 SpringdocRouteBuilder.route() 构建的 RouterFunction 是需要多传一个参数来描述文档的可以在 swagger 看到

          public RouterFunction<ServerResponse> endpoint() {
                  final var tag = "api.console.halo.run/v1alpha1/Post";
                  return SpringdocRouteBuilder.route()
                      .GET("posts", this::listPost, builder -> {
                              builder.operationId("ListPosts")
                                  .description("List posts.")
                                  .tag(tag)
                                  .response(responseBuilder()
                                      .implementation(ListResult.generateGenericClass(ListedPost.class))
                                  );
                              QueryParamBuildUtil.buildParametersFromType(builder, PostQuery.class);
                          }
                      )
          . build();
          }

          如果 Halo 文档有哪里写的不清楚的地方可以去文档项目提 issue

            2 个月 后

            guqing

            halo有一些公司在使用,为公司建设门户站点非常棒,halo的系统框架还可以为中小型公司提供业务系统的支持,只需要编写相应的插件即可。我估计这个路子是不是能加快商业转化,比如购买插件,以及提供插件服务等等。

            我目前正在做这个路子的实施。