【问题标题】:How to use JSON Scalar in SPQR如何在 SPQR 中使用 JSON 标量
【发布时间】:2019-07-22 09:52:09
【问题描述】:

我想在服务类中返回一个 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\"    }  ]}"
  }
}

如何去除转义字符?

【问题讨论】:

    标签: java graphql-java graphql-spqr


    【解决方案1】:

    GraphQL 响应已经是 JSON,因此其中的任何字符串显然都需要适当地转义。 如果要向其中添加动态对象,则必须实际返回一个对象,而不是字符串。对象可以是任何具有正确结构的对象,可以是 Map、Jackson 的 ObjectNode、Gson 的 JsonObject 或 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 ...;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2019-10-12
      • 1970-01-01
      • 2018-12-09
      • 2021-05-28
      • 2019-12-05
      • 2020-08-19
      • 2021-10-26
      • 1970-01-01
      相关资源
      最近更新 更多