jackson jsonSchema:如何为 属性 (JsonRawValue) 设置类型对象

jackson jsonSchema: How to set type object for property (JsonRawValue)

我用jackson and jackson-module-jsonSchema to deserialize json and generate json schema (on fly) to validate json by json-schema-validator.

我有一个 class 字段 "payload"。该字段应包含原始 json,因为可以有客户端需要的任何属性。例如:

{
    "author": "test",
    "payload": {
        "title": "Test title"
    }
}   

我希望字段有效负载在架构中的类型为 "object",但它的类型为 "string"。我应该如何告诉方案生成器使其成为对象???

Class:

import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.JsonNode;

public class Book {
    private String author;
    private Object payload;

    @JsonRawValue
    public Object getPayload() {
        return payload;
    }

    public void setPayload(JsonNode node) {
        this.payload = node;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
            "author='" + author + '\'' +
            ", payload=" + payload +
            '}';
    }
}

我的测试:

@Test
public void generateSchemaBook() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new SimpleModule());
    JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
    final JsonSchema jsonSchema = schemaGen.generateSchema(Book.class);
    jsonSchema.set$schema("http://json-schema.org/draft-03/schema#");
    final String schema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    /*
        {
          "type" : "object",
          "id" : "urn:jsonschema:ru:infon:mas:protocol:Book",
          "$schema" : "http://json-schema.org/draft-03/schema#",
          "properties" : {
            "author" : {
              "type" : "string",
              "required" : true
            },
            "payload" : {
              "type" : "string",
              "required" : true
            }
          }
        }
     */
    System.out.println(schema);
    String testJson = "{\"author\":\"test\",\"payload\":{\"title\":\"Test title\"}}";
    Book book = mapper.readValue(testJson, Book.class);
    System.out.println(book);
    assertEquals("{\"title\":\"Test title\"}", book.getPayload().toString());

    ProcessingReport validate = JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(schema)).validate(JsonLoader.fromString(testJson));
    assertTrue(validate.isSuccess());
}

我没有找到即时执行此操作的解决方案,因此决定生成一次 json 模式,将其放入文件并加载。