自定义swagger的basepath

自定义basePath

new Docket(DocumentationType.SWAGGER_2)
.pathProvider(new IPathProvider(pathProperties))

自定义PathProvider


public class IPathProvider implements PathProvider {
    
    private PathProperties pathProperties;
    
    public IPathProvider(PathProperties pathProperties) {
        this.pathProperties = pathProperties;
    }
    
    /**
     * The base path to the swagger api documentation.
     * <p>
     * Typically docs are served from &lt;yourApp&gt;/api-docs so a relative resourceListing path will omit the api-docs
     * segment. E.g. Relative: "path": "/" Absolute: "path": "http://localhost:8080/api-docs"
     *
     * @return the documentation base path
     */
    protected String getDocumentationPath() {
        return ROOT;
    }
    
    /**
     * The relative path to the operation, from the basePath, which this operation describes. The value SHOULD be in a
     * relative (URL) path format.
     * <p>
     * Includes the apiResourcePrefix
     *
     * @param operationPath operation path
     * @return the relative path to the api operation
     */
    @Override
    public String getOperationPath(String operationPath) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath(pathProperties.getBasePath());
        return removeAdjacentForwardSlashes(uriComponentsBuilder.path(operationPath).build().toString());
    }
    
    /**
     * Corresponds to the path attribute of a swagger Resource Object (within a Resource  Listing).
     * <p>
     * This method builds a URL based off of
     *
     * @param groupName      the group name for this Resource Object e.g. 'default'
     * @param apiDeclaration the identifier for the api declaration e.g 'business-controller'
     * @return the resource listing path
     * @see DefaultPathProvider#getDocumentationPath() by appending the swagger group and apiDeclaration
     */
    @Override
    public String getResourceListingPath(String groupName, String apiDeclaration) {
        String candidate = agnosticUriComponentBuilder(getDocumentationPath()).pathSegment(groupName, apiDeclaration)
                .build().toString();
        return removeAdjacentForwardSlashes(candidate);
    }
    
    private UriComponentsBuilder agnosticUriComponentBuilder(String url) {
        UriComponentsBuilder uriComponentsBuilder;
        if (url.startsWith("http")) {
            uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
        } else {
            uriComponentsBuilder = UriComponentsBuilder.fromPath(url);
        }
        return uriComponentsBuilder;
    }
}

PathProperties

@Data
@ConfigurationProperties("swagger")
public class PathProperties {
    
    private String basePath = "/";
    
}

配置文件

swagger:
  base-path: /miniapp
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值