我想在服务类中返回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" } ]}"
}
}
如何删除逃生字符?
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 ...;
}