首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring Rest文档中添加基本url的路径吗?

在Spring Rest文档中添加基本url的路径吗?
EN

Stack Overflow用户
提问于 2018-12-25 05:30:40
回答 1查看 738关注 0票数 3

我有以下配置可用于Rest文档:

代码语言:javascript
复制
webTestClient = buildWebClient().mutate()
  .filter(documentationConfiguration(restDocumentation))
  .baseUrl("https://api.my-domain.com/")
  .build()

在我的例子中,我将路径前缀用于我的服务- service/foo,因为我使用k8s入口,并且我的服务在路径偏移量上提供服务。

有没有办法在不修改生产代码的情况下插入这样的前缀?

相关文档片段:

https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#configuration-uris-webtestclient

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-28 22:08:43

要记录另一个URI而不是用来生成文档的URI,您必须编写自己的OperationPreprocessor。有一些是预定义的,比如Preprocessors.modifyUris,但它不允许修改请求路径。

查看下面的webTestClient配置和URIUpdaterOperationRequest类。代码可以在GitHub上找到:https://github.com/Query-Interface/SO-Answers/blob/master/java/spring/rest-docs-modify-uripath/src/test/java/com/example/demo/DemoApplicationTests.java

代码语言:javascript
复制
public void init() throws Exception {
    final URIUpdaterPreprocessor preprocessor = new URIUpdaterPreprocessor();
    webTestClient = webTestClient.mutate()
        .filter((documentationConfiguration(this.restDocumentation)
                .operationPreprocessors()
                    .withRequestDefaults(preprocessor)
                    .withResponseDefaults(prettyPrint()))
                )
        .build();
}

private static final class URIUpdaterPreprocessor
    implements OperationPreprocessor {

    @Override
    public OperationRequest preprocess(OperationRequest request) {
        return new URIUpdaterOperationRequest(request);
    }

    @Override
    public OperationResponse preprocess(OperationResponse response) {
        return response;
    }

}

private static final class URIUpdaterOperationRequest
    implements OperationRequest {

    private OperationRequest delegate;

    public URIUpdaterOperationRequest(OperationRequest request) {
        delegate = request;
    }

    public byte[] getContent() {
        return delegate.getContent();
    }

    public String getContentAsString() {
        return delegate.getContentAsString();
    }

    public HttpHeaders getHeaders() {
        return delegate.getHeaders();
    }

    public HttpMethod getMethod() {
        return delegate.getMethod();
    }

    public Parameters getParameters() {
        return delegate.getParameters();
    }

    public Collection<OperationRequestPart> getParts() {
        return delegate.getParts();
    }

    public URI getUri() {
        URI sourceUri = delegate.getUri();
        UriComponentsBuilder builder = UriComponentsBuilder.fromUri(sourceUri);
        return builder
            .host(sourceUri.getHost())
            .replacePath("/service/foo"+sourceUri.getPath())
            .build().toUri();
    }

    public Collection<RequestCookie> getCookies() {
        return delegate.getCookies();
    }
}

我认为另一种可能是更新八字胡模板,以便在所有请求路径引用之前添加前缀。默认模板为located here on github

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53917809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档