0

我想在服务类中返回一个 JSON 文字

@GraphQLQuery(name = "renderUI", description = "Schema for your form")
public String renderUI() {
     String genratedSchema = "{" +
            "  \"schema\": {" +
            "    \"type\": \"object\"," +
            "    \"id\": \"urn:jsonschema:profile:model:DemoForm\"," +
            "    \"properties\": {" +
            "      \"comment\": {" +
            "        \"type\": \"string\"," +
            "        \"title\": \"Comment\"" +
            "      }" +
            "    }" +
            "  }," +
            "  \"form\": [" +
            "    {" +
            "      \"key\": \"comment\"," +
            "      \"type\": \"textarea\"," +
            "      \"required\": false," +
            "      \"description\": \"Add your Comment here\"," +
            "      \"placeholder\": \"fill your comment please\"" +
            "    }" +
            "  ]" +
            "}";
    return  genratedSchema;
}

上面的代码转义了响应中的所有引号

{
  "data": {
    "renderUI": "{  \"schema\": {    \"type\": \"object\",    \"id\": \"urn:jsonschema:com:fnstr:bankprofile:gppbankprofile:model:DemoForm\",    \"properties\": {      \"comment\": {        \"type\": \"string\",        \"title\": \"Comment\"      }    }  },  \"form\": [    {      \"key\": \"comment\",      \"type\": \"textarea\",      \"required\": false,      \"description\": \"Add your Comment here\",      \"placeholder\": \"fill your comment please\"    }  ]}"
  }
}

如何删除转义字符?

4

1 回答 1

2

GraphQL 响应已经是 JSON,因此显然需要对其中的任何字符串进行适当的转义。如果要向其中添加动态对象,则必须实际返回一个对象,而不是字符串。对象可以是任何具有正确结构的对象,可以是 a Map、Jackson's ObjectNode、Gson'sJsonObject或 POJO。

例如

@GraphQLQuery(name = "renderUI", description = "Schema for your form")
public Map<String, Object> renderUI() {
    Map<String, Object> dynamic = new HashMap<>();
    dynamic.put("schema", ...); //fill the whole structure
    return dynamic;
}

或者

@GraphQLQuery(name = "renderUI", description = "Schema for your form")
public ObjectNode renderUI() {
    return ...;
}
于 2019-02-28T16:30:05.583 回答