swagger 2.x过滤器未执行

af7jpaap  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(200)

我正在jersey 2.34 web服务中使用swagger 2.x/openapi 3.0(io.swagger.v3.oas.*)记录一个api。我扩展了io.swagger.v3.core.filter.specfilter,以实现api文档中显示的操作(方法)的自定义筛选。

import io.swagger.v3.core.filter.OpenAPISpecFilter;
import io.swagger.v3.core.filter.SpecFilter;
import io.swagger.v3.oas.models.Operation;

public class ApiAuthorizationFilterImpl extends SpecFilter {

@Override
protected Operation filterOperation(OpenAPISpecFilter filter, Operation operation, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    System.out.println("Filtering operations: "+operation.getSummary());
}

现在,我无法让这个过滤器工作。在my web.xml中添加筛选器表达式时,

<filter>
   <filter-name>ApiAuthorizationFilter</filter-name>
   <filter-class>my.package.ApiAuthorizationFilterImpl</filter-class>
</filter>

<filter-mapping>
   <filter-name>ApiAuthorizationFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

该服务未启动,但在以下情况下失败:

06-Jul-2021 13:01:36.592 SEVERE [http-nio-8080-exec-27] org.apache.catalina.core.StandardContext.filterStart Exception starting filter [ApiAuthorizationFilter]
        java.lang.ClassCastException: class my.package.ApiAuthorizationFilterImpl cannot be cast to class javax.servlet.Filter (my.package.ApiAuthorizationFilterImpl is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @395fdbf6; javax.servlet.Filter is in unnamed module of loader java.net.URLClassLoader @1ab3a8c8)

将筛选器添加到jersey.config.server.provider.classnames时,与其他筛选器一样,它似乎没有被选中。

<servlet>
    <init-param>
      <param-name>jersey.config.server.provider.classnames</param-name>
      <param-value>my.package.ApiAuthorizationFilterImpl;</param-value>
    </init-param>
</servlet>

我真的无法找到方法,尽管有不同方法的样本。执行此筛选的正确方法是什么,或者另一种实现此筛选的方法是什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题