【问题标题】:Render complex object in JSON in grails在 Grails 中以 JSON 格式渲染复杂对象
【发布时间】:2021-10-14 04:38:14
【问题描述】:

我有复杂的对象,我想渲染它,但我有几个问题。

首先,我的班级中有 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\.......

【问题讨论】:

  • 您是否尝试过在没有 gson 的情况下映射响应?您可以填充 Map 以将数据存储为 JSON 并返回。
  • 我做了,但我得到的 JSON 不正确。如果我只发送到像[name: myname, id: myId] as Map 这样的地图,那么它将是正确的,但是如果我尝试render [result: myObject] as Map 其中“myObject”是 MyObject.class 的示例,我会得到错误的渲染
  • 在发送响应之前尝试将数据保存到变量中,并使用 JSON 对象解析它,类似于def test = [test: object.test] as JSON; respond test。检查响应是否运行。检查这个关于stackoverflow.com/questions/22601188/…的问题
  • 我添加了 3 个尝试示例。问题在于,我从另一个端点的休息请求中获得了该对象,将 LazyMap 转换为我的 ExampleObject 执行一些逻辑并且必须将我的对象发送到前面。 ExampleObject 不是域对象

标签: json grails


【解决方案1】:

我是这样做的

@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": [
            { ....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多