0

I am having the JAXRS application, where I want to expose the REST API via Swagger.

The configuration is the same as for the sample (Swagger JAX RS sample).

It had worked fine until I added several filters (javax.servlet.Filter and javax.ws.rs.container.ContainerRequestFilter) required for my application.

Filters work on /api/* path, and Swagger reads the documentation from /api/api-docs path.

Is it somehow possible to avoid filters for Swagger?

Jurica Krizanic
  • 1,072
  • 2
  • 11
  • 26
  • I believe [this][1] StackOverflow question answers your question. [1]: http://stackoverflow.com/questions/3125296/can-i-exclude-some-concrete-urls-from-url-pattern-inside-filter-mapping – Ron Dec 10 '14 at 10:23
  • It solves javax.servlet.Filter problem, but how to skip javax.ws.rs.container.ContainerRequestFilter filters? – Jurica Krizanic Dec 10 '14 at 11:29
  • Possibly http://stackoverflow.com/questions/27043093/jax-rs-retrieve-path-pattern-in-containerrequestfilter – Ron Dec 10 '14 at 11:36
  • Already have URI. I would like to skip all filters when reading /api/api-docs path without modifying every filter and checking the URI. – Jurica Krizanic Dec 10 '14 at 12:07
  • I don't think there's a centralized solution for that. I believe it's filter-specific. – Ron Dec 10 '14 at 12:33
  • Please refer the following link. https://stackoverflow.com/questions/66740706/solution-swagger-api-of-spring-boot-restful-web-service-is-not-working-due-to – Anupam Mar 22 '21 at 05:27
  • Please refer the following link. https://stackoverflow.com/questions/66740706/solution-swagger-api-of-spring-boot-restful-web-service-is-not-working-due-to – Anupam Mar 22 '21 at 05:28

1 Answers1

0

Found the solution. In web.xml I added the filter which handles the /api/api-docs/* request. It is set before filters which handle /api/* requests.

<filter>
    <filter-name>SwaggerDocRequestFilter</filter-name>
    <filter-class>my.package.filters.SwaggerDocRequestFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SwaggerDocRequestFilter</filter-name>
    <url-pattern>/api/api-docs/*</url-pattern>     
    <dispatcher>REQUEST</dispatcher>   
</filter-mapping>

SwaggerDocRequestFilter looks like this:

    public class SwaggerDocRequestFilter implements Filter {

    @Override
    public void init(FilterConfig fc) throws ServletException {        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String path = ((HttpServletRequest) request).getRequestURI();
        if (path.contains("/api-docs")) {
            String forwardPath = ((HttpServletRequest) request).getServletPath() + 
                    StringUtils.defaultString(((HttpServletRequest)request).getPathInfo());

            request.getRequestDispatcher(forwardPath).
                    forward(request, response); 
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {} 
}

This overrides all javax.servlet.Filter implementations.

For overriding JAX RS filters, it is required to provide own implementation of javax.ws.rs.container.DynamicFeature interface. This implementation applies filters only when requests to specific resources occurrs, in my case for resources in a specific package.

    public class MyDynamicFiltersRegister implements DynamicFeature {

@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext context) {
        if (resourceInfo.getResourceClass().getPackage().getName().equals("my.package.resources))   {
             //register filters on FeatureContext objec only if resource in specific package.
             ...
        }
    }
}

In this case, Swagger UI works fine. On requests to specific resources, JAX RS filters are applied.

Jurica Krizanic
  • 1,072
  • 2
  • 11
  • 26