【问题标题】:Swagger API of spring boot RESTful web service is not working due to servlet filterSpring Boot RESTful Web 服务的 Swagger API 由于 servlet 过滤器而无法正常工作
【发布时间】:2021-03-22 05:21:51
【问题描述】:

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

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

【问题讨论】:

    标签: java spring swagger-ui


    【解决方案1】:

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

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

    【讨论】:

    • 能否提供完整的解决方案
    猜你喜欢
    • 2013-08-16
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多