我创建了一个 groovy DSL 合约,如下所示
import org.springframework.cloud.contract.spec.Contract
Contract.make {
final def NAME_REGEX = '[A-Za-z0-9\\u00C0-\\u00FF\'\\- ]{1,70}'
request {
method 'GET'
url('/api/getEmployess') {
queryParameters {
parameter 'name': $(c(regex(NAME_REGEX)), p('\u00CAdward J\u00F5hnson'))
}
}
headers {
contentType("application/json;charset=UTF-8")
}
}
response {
status OK()
body([
[
id : $(p(regex(nonBlank())), c('5a0eaf2012a9a12f1c98947a')),
name : fromRequest().query("name")
]
])
headers { contentType("application/json;charset=UTF-8") }
}
}
我的服务实现返回“name”和“id”作为响应。作为响应,'name' 是 Unicode 值 'Êdward Jõhnson',它与请求参数值不匹配。
我得到以下错误 -
Parsed JSON [[{"id":"5a0eaf2012a9a12f1c98947a","name":"Êdward Jõhnson"}]] doesn't match the JSON path [$[*][?(@.['name'] == 'Êdward Jõhnson')]]
java.lang.IllegalStateException: Parsed JSON [[{"id":"5a0eaf2012a9a12f1c98947a","name":"Êdward Jõhnson" }]] doesn't match the JSON path [$[*][?(@.['name'] == 'Êdward Jõhnson')]]
at com.toomuchcoding.jsonassert.JsonAsserter.check(JsonAsserter.java:228)
at com.toomuchcoding.jsonassert.JsonAsserter.checkBufferedJsonPathString(JsonAsserter.java:267)
我尝试在“名称”请求查询参数中以两种方式传递 Unicode 值 -
- 将 Unicode 字符作为 Unicode 数字,如上例 - 参数 'name': $(c(regex(NAME_REGEX)), p(' \u00CAdward J\u00F5hnson '))
- 将Unicode字符作为参数'name': $(c(regex(NAME_REGEX)), p(' Êdward Jõhnson '))
但是对于这两种情况,我都会遇到同样的错误。看起来有些编码问题,因为我的值Êdward Jõhnson更改为Êdward Jõhnson,如错误中所述。
请帮我解决这个问题。