swagger api由于servlet过滤器而无法工作

olhwl3o2  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(375)

servlet过滤器会抑制许多swagger响应(包括图像和css),以便在浏览器上创建swagger ul gui屏幕。请通过以下代码段绕过此类行为。

@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);
    } 
}
vsdwdz23

vsdwdz231#

我们需要添加上述过滤器,在我们的spring引导控制器中,我们需要-

@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;
}

相关问题