Swagger 2.x 过滤器未执行 [英] Swagger 2.x filter not executed

查看:28
本文介绍了Swagger 2.x 过滤器未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jersey 2.34 Web 服务中使用 Swagger 2.x/OpenAPI 3.0 (io.swagger.v3.oas.*) 记录 API.我已扩展 io.swagger.v3.core.filter.SpecFilter 以实现操作(方法)的自定义过滤以显示在 API 文档中.

I am documenting an API with Swagger 2.x/ OpenAPI 3.0 (io.swagger.v3.oas.*) in a Jersey 2.34 web service. I have extended io.swagger.v3.core.filter.SpecFilter to implement a custom filtering of Operations (methods) to show in the API docs.

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());
}

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

Now, I am not able to get this filter working. When adding a filter expression in 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 时,就像其他过滤器一样,它似乎没有被选中.

When adding the filter to jersey.config.server.provider.classnames, as done with other filters, it just does not seem to get picked up.

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

我真的无法找到要走的路,即使有不同方法的样本.执行此过滤器的正确方法或实现此过滤的另一种方法是什么?

I wasn't really able to find the way to go, even though there are samples out there with different approaches. What would be the correct way to get this filter executed or alternatively another approach to implement this filtering?

推荐答案

既然我现在可以解决这个问题,我就把它贴在这里作为答案.

Since I could figure this out now, I will post it here as an answer.

我通过深入了解 Swagger/OpenAPI 源代码意识到,这个过滤器应该写成

As I realized by diving a bit into the Swagger/ OpenAPI source code, this filter should be written as

import io.swagger.v3.core.filter.AbstractSpecFilter;
import io.swagger.v3.core.model.ApiDescription;
import io.swagger.v3.oas.models.Operation;

public class ApiAuthorizationFilterImpl extends AbstractSpecFilter {
    @Override
    public Optional<Operation> filterOperation(Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
   }
}
    

然后将其作为过滤器类添加到 openapi.yaml 文档中来执行

It will then get executed by adding it into the openapi.yaml document as a filterClass

prettyPrint: true
cacheTTL: 0
readAllResources: false
filterClass: my.package.ApiAuthorizationFilterImpl 
openAPI:
  info:
...

这篇关于Swagger 2.x 过滤器未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆