Spring 来自文件的云合同 HTTP 输出主体来自请求动态

Spring Cloud Contract HTTP output body from file dynamically from request

如果可以动态地从请求中获取响应文件名,那么为模拟服务器提供 "test data" 将是一种不错且敏捷的方法。我想知道是否有人可以找到某种解决方法来实现类似 :

import org.springframework.cloud.contract.spec.Contract

Contract.make {

    request {
        url "/one/two/three/response"
        method GET()
    }

    response {
        status OK()
        headers {
            contentType applicationJson()
        }
        body ( file( '' + $(consumer(fromRequest().path(3)), producer("response")) + '.json'))
    }
}

或者只是无法根据请求动态确定输出文件?

不幸的是你不能做这样的事情。您必须手动创建正文,其部分应引用 fromRequest.

您可以从单独的 json 文件中引用请求,以将值放入特定的 属性。例如,在 .json 文件中,您可以执行类似

的操作
{
   "foo": "{{{ request.path.[3] }}}"
}

但是你的建议是行不通的。您不能动态创建或 select 不同的 json 文件。