gpt4 book ai didi

java - Spring 合约 : how to send a Collection of Strings as a RequestBody

转载 作者:行者123 更新时间:2023-12-01 19:36:40 26 4
gpt4 key购买 nike

关于如何为使用 @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;
}

并且我出于测试目的编写了以下契约(Contract):


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,测试找不到我的方法,我想我的错误可能是我发送字符串集合的方式,我尝试了一些其他选项但没有成功。

最佳答案

我尝试了上面的建议,但不幸的是它们都没有解决我的问题。我得到 415 的原因是,当我向请求添加正文时,也会在幕后对正文的内容类型进行检查,因此我必须在请求中显式指定正文采用 json 格式:

request {
method POST()
url("/some/uri?key=NEW_KEY")
headers {header 'Content-Type': 'application/json;charset=UTF-8'}
body'''["12345",
"00143"]'''
}

关于java - Spring 合约 : how to send a Collection of Strings as a RequestBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59211459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com