【发布时间】:2019-12-06 10:36:07
【问题描述】:
关于如何为使用@RequestBody 注释的方法编写合约的问题,该方法将字符串集合作为参数。 我有以下方法:
@PostMapping(path = "/some/uri", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiOperation("GET with body")
public Response<Boolean> someMethod(@RequestParam(value = "key") final String key,
@RequestBody final Collection<String> numbers){
return some logic;
}
并且我已经为测试目的编写了以下合同:
import org.springframework.cloud.contract.spec.Contract
Contract.make {
description "Should return true"
request {
method POST()
url("/some/uri?key=NEW_KEY")
body'''["12345",
"00143"]'''
}
response {
status 200
headers {header 'Content-Type': 'application/json;charset=UTF-8'}
body '''true'''
}
我一直收到 415,测试找不到我的方法,我想我的错误可能是我发送字符串集合的方式,我尝试了一些其他选项但没有成功。
【问题讨论】:
-
您可以尝试使用您的密钥和数字集合作为其中的字段来创建一个 DTO,然后使用 @RequestBody 将其作为参数传递给您的方法
-
body 没问题,试试用
List代替Collection。
标签: java spring groovy contract