首页 > 解决方案 > Spring Cloud Contract 无法转换 contract.yml 中的 unicode 字符串

问题描述

我想要来自 YAML 编写的合同文件的 WireMock 存根的 JSON 文件。
但是 Spring Cloud Contract Gradle Plugin 在包含 unicode 字符串(例如“日本”)时无法正确转换合约文件。
(我通过执行 Gradle 插件的“generateClientStubs”任务来生成存根。)

具体来说,我制作了以下合约 yaml 文件。

name: get-all-users
request:
url: /users
method: GET
response:
status: 200
headers:
    content-type: application/json; charset=UTF-8
body:
    -
    id: 001
    name: alice
    country: US
    - 
    id: 002
    name: bob
    country: 日本

在完成 generateClientStubs 任务后,我得到了以下 JSON 文件。

{
 "id" : "b776c7d1-fd6d-454a-be92-ef02b8eec793",
 "request" : {
     "url" : "/users",
     "method" : "GET"
 },
 "response" : {
     "status" : 200,
     "body" : "[{\"id\":1,\"name\":\"alice\",\"country\":\"US\"},{\"id\":2,\"name\":\"bob\",\"country\":\"\\u65e5\\u672c\"}]",
     "headers" : {
     "content-type" : "application/json; charset=UTF-8"
     },
     "transformers" : [ "response-template" ]
 },
 "uuid" : "b776c7d1-fd6d-454a-be92-ef02b8eec793"
}

如您所见,响应正文的 unicode 字符串(尤其是 "country\":\"\u65e5\u672c\" 部分)未正确转换。我想要的是如下:

"body" : "[{\"id\":1,\"name\":\"alice\",\"country\":\"US\"},{\"id\":2,\"name\":\"bob\",\"country\":\"日本"}]"

我必须处理这样的日语字符串(因为我是日本人......)。
我该如何解决它们?

感谢您的阅读。

标签: javaspringspring-bootspring-cloud-contract

解决方案


推荐阅读