【问题标题】:How to include Bean Validation constraints in Jackson generated JSON schema如何在杰克逊生成的 JSON 模式中包含 Bean 验证约束
【发布时间】:2013-01-03 12:39:07
【问题描述】:

我正在使用 Jersey 1.2(仍在使用 JDK 1.5)并开发了一个 REST Booking Web 资源和一个相关的 Booking POJO。

我使用 Bean Validation 来限制字段的大小/类型,例如

@NotNull
@Size(message="invalid size",min=3,max=20)
@Pattern(message="invalid pattern",regexp = "^[A-Za-z]*")
@JsonProperty(value = "forename")
private String forename;

并使用了 Jackson ObjectMapper.generateJsonSchema 类来生成 JSON 模式,但是这忽略了所有验证注释,所以我只得到:

"forename" : {
  "type" : "string"
}

有没有办法将限制信息包含在生成的架构中?

非常感谢

【问题讨论】:

    标签: json jackson bean-validation


    【解决方案1】:

    看起来 jackson-module-jsonSchema 2.5.0+ 现在支持一些注释:

    ValidationSchemaFactoryWrapper personVisitor = new    
    ValidationSchemaFactoryWrapper();
    ObjectMapper mapper = new ObjectMapper();
    mapper.acceptJsonFormatVisitor(Person.class, personVisitor);
    JsonSchema personSchema = personVisitor.finalSchema();
    

    【讨论】:

      【解决方案2】:

      没有办法做到开箱即用,但是这个扩展模块:

      https://github.com/FasterXML/jackson-module-jsonSchema

      应该让您修改模式生成器以添加您想要的任何额外信息。 警告:这是一个新项目,可能需要您使用 Jackson 核心组件的 2.2.0-SNAPSHOT 版本。

      【讨论】:

      • 非常感谢回复,我去看看试试看。已对答案投了赞成票,一旦我完成尝试,将标记为正确!
      【解决方案3】:

      只是说这就是我解决此问题的方法。我使用了非常好的带有标志的 jsonschema2pojo 插件 - includeJsr303Annotations

      <build>
              <plugins>
                  <plugin>
                      <groupId>org.jvnet.jaxb2.maven2</groupId>
                      <artifactId>maven-jaxb2-plugin</artifactId>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-source-plugin</artifactId>
                  </plugin>
                  <plugin>
                      <groupId>org.jsonschema2pojo</groupId>
                      <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                      <version>0.4.11</version>
                      <configuration>
                          <includes>
                              <include>*.json</include>
                          </includes>
                          <useJodaDates>true</useJodaDates>
                          <sourceDirectory>${basedir}/src/main/resources/webapi/jsonschema</sourceDirectory>
                          <targetPackage>com.ba.schema.json.jsonschemav02</targetPackage>
                          <includeJsr303Annotations>true</includeJsr303Annotations>
                          <includeHashcodeAndEquals>false</includeHashcodeAndEquals>
                          <includeToString>false</includeToString>
                          <outputDirectory>${project.build.directory}/generated-sources/xjc/</outputDirectory>
                      </configuration>
                      <executions>
                          <execution>
                              <goals>
                                  <goal>generate</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      

      我还没有尝试过 jackson-module-jsonSchema 2.5.0+,因为上面的内容满足了我的需求。感谢大家的帮助。

      【讨论】:

      • 您能否将验证错误消息返回到架构?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 2016-07-21
      • 1970-01-01
      • 2015-06-21
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多