json - 在 Grails 中以 JSON 格式渲染复杂对象
问题描述
我有复杂的对象,我想渲染它,但我有几个问题。
首先,我的班级中有 UUID 字段,但在视图中我得到的不是字符串,而是 mostSigBits 和 leastSigBits。第二个,我有我的枚举字段,比如两个带有枚举和值的字段
例如,
public class ExampleObject {
@JsonProperty("id")
private UUID id;
@JsonProperty("name")
private String name;
@JsonProperty("address")
private String address;
@JsonProperty("port")
private String port;
@JsonProperty("users")
@Valid
private List<UserRef> users = null;
@JsonProperty("indexingParameters")
private IndexingParameters indexingParameters;
@JsonProperty("otherParameters")
private OtherParameters otherParameters;
@JsonProperty("status")
private Status status;
}
当我得到控制器的响应时,我得到了这个答案
{
"id": {
"leastSignificantBits": -5406850341911646206,
"mostSignificantBits": 8884977146336383467
},
"status": {
"enumType": "api.model.Status",
"name": "GENERAL"
}
....
}
问题是我的代码中有很多不同但问题对象相同。如果只有 1 个对象,我很容易准备一些 _exampleObject.gson 模板并将控制器的每个答案呈现给它,但我有很多对象。
我认为有一些变体可以正确呈现我的 JSON,不是吗?
另一个渲染变体data
是 ExampleObject.class 或类似的东西
1)代码:
Map map = [content: data.content, sorting: data.sorting, paging: data.paging] as Map
render AppResponse.success([success: true, data: map]).asJSON()
render data as JSON
正面:
Incorrect UUID and DateTime convert each field in Object, But I need Timeshtamp
"id": {"leastSignificantBits": -5005002633583312101,
"mostSignificantBits": 4056748206401340307},
"key": "b48d35dd-0551-4265-a1b1-65105e713811",
2)代码:
Map map = [data: new ObjectMapper().writeValueAsString(data)] as Map
render map
正面:
Here we can see square brackets at the start which is wrong for JSON
['data':'{"content":[{"id":"384c7700-09c1-4393-ba8a-a89f555f431b","name":"somename"...
3)代码:
Object result = new HashMap<String, Object>()
result.success = true
result["data1"] = new ObjectMapper().writeValueAsString(data)
render result as JSON
正面:
Here we can see quotes escaping
"data": "{\"content\":[{\"id\":\"384c7700-09c1-4393-ba8a-a89f555f431b\",\"name\":\"somename\",\"key\":\"b48d35dd-0551-4265-a1b1-65105e713811\",\"status\":\"COMPLETED\.......
解决方案
我是这样做的
@CompileStatic
class MyProxyController {
@Autowired
Myservice service
static ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JodaModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
def getExampleObject {
ExampleObject exampleObject = service.getExampleObject()
render([contentType: "application/json"], objectMapper.writeValueAsString(new CustomObject(data, true)))
}
@CompileStatic
class CustomObject {
Boolean success
Object data
CustomObject(Object data, Boolean success) {
this.data = data
this.success = success
}
}
}
并得到我想要的json
{
"success": true,
"data": {
"content": [
{ ....
推荐阅读
- node.js - 是否有理由在 Gulp 配置文件中使用 var 而不是 const ?
- java - 仅在 charAt() 和 isSpaceChar() 的帮助下,在 java 中使第一个后者大写
- uwp - 为什么 Microsoft.NET.CoreRuntime.1.1.appx 依赖于 - UWP APPX
- html - Thymeleaf:禁用组件
- gradle - 如何将工件从 gradle 上传到 nexus 存储库?
- azure - 发布 Azure 函数
- java - 如何按对流的键进行分组
- javascript - 路由器在 NavigationStart 事件中导航
- python - -m 选项在 python 中代表什么?
- java - Java RestfulService 不返回 JSONArray