Spring Boot RESTful Web 服务的 Swagger API 由于 servlet 过滤器而无法工作 [英] Swagger API of spring boot RESTful web service is not working due to servlet filter

查看:67
本文介绍了Spring Boot RESTful Web 服务的 Swagger API 由于 servlet 过滤器而无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

servlet 过滤器抑制许多 swagger 响应(包括图像和 CSS)以在浏览器上创建 swagger-ul GUI 屏幕.请通过以下代码片段绕过此类行为.

The servlet filter suppresses many swagger responses (including images and CSS) to create a swagger-ul GUI screen on the browser. Please bypass such acts by the following code snippet.

@Component
public class DomainAuthorizationFilter implements Filter {


    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        if(httpRequest.getRequestURI().trim().toLowerCase().matches(".*swagger-.*|.*api-docs.*"))
        {           request.getRequestDispatcher(httpRequest.getServletPath()).forward(request, response);
            return;
        }
        else {
              // Your login in the filter.
        }
        chain.doFilter(request, response);
    } 
}

推荐答案

我们需要添加上述过滤器,并且在我们的 spring-boot 控制器中,我们需要放像 -

We need to add the said filter and inside our spring-boot controller, we need to put like -

@ApiOperation(value = "description of method.", response = Object.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "record found."),
        @ApiResponse(code = 404, message = "record not found") })
@ApiImplicitParams(value = {
        @ApiImplicitParam(name = "header_1", value = "Provide header_1 in the header.", required = true, dataType = "string", paramType = "header"),
        @ApiImplicitParam(name = "header_2", value = "Provide header_2 in the header.", required = true, dataType = "string", paramType = "header") })  
@GetMapping(value = "/getRecord", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseDTO<AccountDashboardDTO> getRecord()
{
    // Your business logic.
    return response;
}

这篇关于Spring Boot RESTful Web 服务的 Swagger API 由于 servlet 过滤器而无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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