SpringCloud Contract DSL
2023-12-12 17:57 更新
Spring Cloud Contract支持2种类型的DSL。一种写在Groovy中,另一种写在YAML中。
如果您决定将合同写在Groovy中,那么如果您以前没有使用过Groovy,请不要惊慌。确实不需要语言知识,因为Contract DSL仅使用它的一小部分(仅文字,方法调用和闭包)。同样,DSL是静态类型的,以使其在不了解DSL本身的情况下就可以被程序员读取。
请记住,在Groovy合同文件中,必须为
Contract类和make静态导入(例如org.springframework.cloud.spec.Contract.make { … })提供完全限定名称。您还可以导入Contract类:import org.springframework.cloud.spec.Contract,然后调用Contract.make { … }。
Spring Cloud Contract支持在单个文件中定义多个合同。
以下是YAML合同定义的完整示例:
description: Some description
name: some name
priority: 8
ignored: true
request:
url: /foo
queryParameters:
a: b
b: c
method: PUT
headers:
foo: bar
fooReq: baz
body:
foo: bar
matchers:
body:
- path: $.foo
type: by_regex
value: bar
headers:
- key: foo
regex: bar
response:
status: 200
headers:
foo2: bar
foo3: foo33
fooRes: baz
body:
foo2: bar
foo3: baz
nullValue: null
matchers:
body:
- path: $.foo2
type: by_regex
value: bar
- path: $.foo3
type: by_command
value: executeMe($it)
- path: $.nullValue
type: by_null
value: null
headers:
- key: foo2
regex: bar
- key: foo3
command: andMeToo($it)您可以使用独立的maven命令将合同编译为存根映射:
mvn org.springframework.cloud:spring-cloud-contract-maven-plugin:convert
以上内容是否对您有帮助:

免费 AI IDE


更多建议: