![](/img/trans.png)
[英]How to configure GraphQL SPQR to use Gson instead of Jackson
[英]How to use JSON Scalar in SPQR
我想在服务类中返回一个 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 ...;
}
我认为@kaqqao的解决方案是最佳做法。 但是,如果您只想返回一个简单的字符串,请使用旧的字符串替换:
return genratedSchema.replace("\\", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.