我正在为一个 http 请求编写一个spring 云合同,它的一个 json 主体(我们称之为myMap)应该是一个非空映射(字符串到字符串)。所以请求者(又名消费者)必须有这样的东西:“myMap”:{“key”:“val”}
是否可以在合同中强制执行此类内容?
这是我为确保上下文而编写的现有合同的示例:
package contracts
org.springframework.cloud.contract.spec.Contract.make {
description("""
Represents a successful scenario of registering new host
given:
hostProperties are valid
then:
we'll register the host
""")
request {
method 'POST'
urlPath value(consumer(~/\/api\/hosts\/[a-zA-Z0-90-9]+/), producer('/api/hosts/icsl7875'))
body([
timeStamp : $(consumer(anyNumber()), producer(334)),
hyperThreaded : $(consumer(regex('^(true|false)$')), producer(false)),
virtualMachine: $(consumer(regex('^(true|false)$')), producer(false)),
poolName : $(consumer(regex('(.+)')), producer("dev_regression")),
osImage : $(consumer(regex('(.+)')), producer("osImage1")),
cores : $(consumer(anyNumber()), producer(2)),
memory : $(consumer(anyNumber()), producer(256)),
osRelease : $(consumer(regex('(.+)')), producer("osRelease1")),
wsmVeriosn : $(consumer(regex('(.+)')), producer("8.2.16")),
cpuCount : $(consumer(anyNumber()), producer(2445L)),
cpuMhz : $(consumer(anyNumber()), producer(22354L)),
cpuMips : $(consumer(anyNumber()), producer(256F))
])
headers {
contentType(applicationJsonUtf8())
}
}
response {
status 201
body([
groupId: $('067e6162-3b6f-4ae2-a171-2470b63dff00')
])
headers {
contentType(applicationJson())
}
}
}