我试图编写自己的Swagger API过滤器来隐藏某些内容,使其不会出现在API中。我想我对应该做什么有很好的了解,但我必须在细节中遗漏一些东西。
我使用的是Swagger Core 1.3.10和Spring 3.1。这是我现在拥有的一些内容:
SwaggerApiAuthorizationFilter
public class SwaggerApiAuthorizationFilter implements SwaggerSpecFilter {
private final static Logger logger = LoggerFactory.getLogger(SwaggerApiAuthorizationFilter.class);
@Override
public boolean isOperationAllowed(Operation operation, ApiDescription api, Map<String, List<String>> params,
Map<String, String> cookies, Map<String, List<String>> headers) {
return true;
}
@Override
public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription api, Map<String, List<String>> params,
Map<String, String> cookies, Map<String, List<String>> headers) {
// do not allow the documentation to be generated on parameters that have their access set to "internal"
if (parameter.paramAccess().isDefined() && parameter.paramAccess().get().equalsIgnoreCase("internal")) {
logger.debug("The following parameter has been hidden from the Swagger API documentation: " + parameter.name());
return false;
} else {
return true;
}
}
}
SwaggerServlet
public class SwaggerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final static Logger logger = LoggerFactory.getLogger(SwaggerServlet.class);
BeanConfig beanConfig;
SwaggerApiAuthorizationFilter swaggerApiAuthorizationFilter;
public void setBeanConfig(BeanConfig beanConfig) {
this.beanConfig = beanConfig;
}
public void setSwaggerApiAuthorizationFilter(SwaggerApiAuthorizationFilter swaggerApiAuthorizationFilter) {
this.swaggerApiAuthorizationFilter = swaggerApiAuthorizationFilter;
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
try {
super.init(servletConfig);
beanConfig.setBasePath("/mbl/services");
beanConfig.setVersion("1.0");
beanConfig.setResourcePackage("com.whatever.resources");
beanConfig.setScan(true);
FilterFactory.setFilter(swaggerApiAuthorizationFilter);
logger.debug("The Swagger servlet has been initialized");
} catch (Exception e) {
e.printStackTrace();
}
}
}
弹簧配置文件
<!-- Swagger Configuration and Providers -->
<bean id="beanConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
<property name="title" value="Java API"/>
<property name="version" value="1.0" />
<property name="basePath" value="/mbl/services"/>
<property name="resourcePackage" value="com.whatever.resources"/>
<property name="scan" value="true"/>
</bean>
<bean id="swaggerApiAuthorizationFilter" class="com.whatever.util.SwaggerApiAuthorizationFilter" />
的web.xml
<!-- Enabling Swagger servlet -->
<servlet>
<servlet-name>Swagger Servlet</servlet-name>
<servlet-class>com.whatever.web.SwaggerServlet</servlet-class>
<init-param>
<param-name>swagger.filter</param-name>
<param-value>com.whatever.util.SwaggerApiAuthorizationFilter</param-value>
</init-param>
<load-on-startup>-1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Swagger Servlet</servlet-name>
<url-pattern>/api-docs</url-pattern>
</servlet-mapping>
资源类
public Response getUserInfo(
@Context HttpHeaders headers,
@ApiParam(name="brand", access="internal") @CookieParam(value = "brand") String brand) {
看来我的SwaggerApiAuthorizationFilter甚至没有被调用。我的猜测是我的配置在web.xml或SwaggerServlet中出错了。有没有人看到上面看起来不正确或有没有任何想法?
答案 0 :(得分:1)
我刚刚成功使用了过滤器。我有问题使过滤功能正常。问题是找到我的过滤器的实现类是一个问题。当我将包改为swagger类可以访问的地方时,所有内容都正确无误。接下来,我只需要在我的过滤器类上实现我的逻辑。我的过滤器类也实现了SwaggerSpecFilter接口。最好的问候。
我正在使用DefaultJaxrsConfig:
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>/my-service/service/</param-value>
</init-param>
<init-param>
<param-name>swagger.filter</param-name>
<param-value>mypackage.SwaggerFilter</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
答案 1 :(得分:0)
我只是在猜测,但我认为你需要实现Filter以及SwaggerSpecFilter接口,而不是扩展HttpServlet(看看它没有出现SwaggerSpecFilter扩展过滤器的源代码,然后将其配置为过滤器)在您的web.xml而不是servlet中。它会是这样的:
<filter>
<filter-name>Swagger Filter</filter-name>
<filter-class>com.whatever.util.SwaggerApiAuthorizationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Swagger Filter</filter-name>
<url-pattern>/api-docs</url-pattern>
</filter-mapping>
同样,不确定这是否会起作用,但只是猜测我的头脑。