如何在 SPQR 中使用 JSON 标量

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

  }

}

如何删除转义字符?


慕森卡
浏览 81回答 1
1回答

慕码人8056858

GraphQL 响应已经是 JSON,因此显然需要对其中的任何字符串进行适当的转义。如果要向其中添加动态对象,则必须实际返回一个对象,而不是字符串。对象可以是任何具有正确结构的对象,可以是 a Map、Jackson's ObjectNode、Gson'sJsonObject或 POJO。例如@GraphQLQuery(name = "renderUI", description = "Schema for your form")public Map<String, Object> renderUI() {&nbsp; &nbsp; Map<String, Object> dynamic = new HashMap<>();&nbsp; &nbsp; dynamic.put("schema", ...); //fill the whole structure&nbsp; &nbsp; return dynamic;}或者@GraphQLQuery(name = "renderUI", description = "Schema for your form")public ObjectNode renderUI() {&nbsp; &nbsp; return ...;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java