创建JSON对象的JSON列表

时间:2017-06-10 08:52:43

标签: java json spring-boot jackson json-schema-validator

我在Spring Boot中构建了一个REST服务,接受JSON POST到端点。我正在使用JSON验证针对POST架构的JSON json-schema-validator版。当出现验证错误时,我会在JSON中收到错误消息。我想将错误消息发送回JSON中的客户端。我遇到的问题是我在自己的JSON对象中收到每条错误消息。这不是一个问题,它只有一个错误消息,但我想只有一条JSON消息,如果有多个错误,则包含所有错误消息。目前,我在ProcessingReport对象中收到错误消息。迭代ProcessingReport我能够获得可以转换为ProcessingMessage的每个JSON

for(ProcessingMessage message : report ) {
                  message.asJson();
            }

每条错误消息如下所示:

{
    "level": "error",
    "schema": {
      "loadingURI": "#",
      "pointer": "/properties/parameterDateAdjusted"
    },
    "instance": {
      "pointer": "/parameterDateAdjusted"
    },
    "domain": "validation",
    "keyword": "format",
    "attribute": "date-time",
    "message": "string \"2017-02-29T23:59:00Z\" is invalid against requested date format(s) [yyyy-MM-dd'T'HH:mm:ssZ, yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z]",
    "value": "2017-02-29T23:59:00Z",
    "expected": [
      "yyyy-MM-dd'T'HH:mm:ssZ",
      "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z"
    ]
  }

我想有多条错误消息,如下所示:

{
  "error": 1,
  "message": {
    "level": "error",
    "schema": {
      "loadingURI": "#",
      "pointer": "/properties/parameterDateAdjusted"
    },
    "instance": {
      "pointer": "/parameterDateAdjusted"
    },
    "domain": "validation",
    "keyword": "format",
    "attribute": "date-time",
    "message": "string \"2017-02-29T23:59:00Z\" is invalid against requested date format(s) [yyyy-MM-dd'T'HH:mm:ssZ, yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z]",
    "value": "2017-02-29T23:59:00Z",
    "expected": [
      "yyyy-MM-dd'T'HH:mm:ssZ",
      "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z"
    ]
  },
  "error": 2,
  "message": {
    "level": "error",
    "schema": {
      "loadingURI": "#",
      "pointer": "/properties/parameterDateUnadjusted"
    },
    "instance": {
      "pointer": "/parameterDateUnadjusted"
    },
    "domain": "validation",
    "keyword": "format",
    "attribute": "date-time",
    "message": "string \"2010-11-31T00:00:00Z\" is invalid against requested date format(s) [yyyy-MM-dd'T'HH:mm:ssZ, yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z]",
    "value": "2010-11-31T00:00:00Z",
    "expected": [
      "yyyy-MM-dd'T'HH:mm:ssZ",
      "yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z"
    ]
  }
}

这样做的最佳方法是什么?我可以创建一个List JSON List<Jsonnode>对象,例如Jsonnode和转换为JSON的对象吗?或者我可以通过某种方式连接x错误消息?

2 个答案:

答案 0 :(得分:0)

您只需返回一个对象列表,Spring会将其映射到JSON数组。

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody
List<ErrorWrapper> getSomething() throws IOException {
    List<JsonNode> errorNodes = new ArrayList<>();
    errorNodes.add(new ObjectMapper().createObjectNode());
    errorNodes.add(new ObjectMapper().createObjectNode());

    return IntStream.range(0, errorNodes.size())
            .mapToObj(i -> new ErrorWrapper(i + 1, errorNodes.get(i)))
            .collect(Collectors.toList());
}

public class ErrorWrapper {
    private int error;
    private JsonNode message;

    public ErrorWrapper(int error, JsonNode message) {
        this.error = error;
        this.message = message;
    }

    public int getError() {
        return error;
    }

    public JsonNode getMessage() {
        return message;
    }
}

返回

[
    {
        "error": 1,
        "message": { }
    },
    {
        "error": 2,
        "message": { }
    }
]

答案 1 :(得分:0)

我接受了卢西亚诺的部分答案并提出了这个问题:

access_token

它创建了我想要的响应。我不确定是否有任何问题我不知道。