Man*_*dyW 6 json jackson bean-validation
我正在使用Jersey 1.2(仍然使用JDK 1.5)并开发了REST预订Web资源和相关的预订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;
Run Code Online (Sandbox Code Playgroud)
并使用Jackson ObjectMapper.generateJsonSchema类生成JSON模式但是这忽略了所有验证注释,所以我得到:
"forename" : {
"type" : "string"
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将限制信息包含在生成的模式中?
非常感谢
没有办法开箱即用,但这个扩展模块:
https://github.com/FasterXML/jackson-module-jsonSchema
应该允许您以一种方式修改Schema Generator,以添加您想要的任何额外信息.警告:这是一个新项目,可能需要您使用2.2.0-SNAPSHOT版本的核心Jackson组件.
小智 5
看起来jackson-module-jsonSchema 2.5.0+现在支持一些注释:
ValidationSchemaFactoryWrapper personVisitor = new
ValidationSchemaFactoryWrapper();
ObjectMapper mapper = new ObjectMapper();
mapper.acceptJsonFormatVisitor(Person.class, personVisitor);
JsonSchema personSchema = personVisitor.finalSchema();
Run Code Online (Sandbox Code Playgroud)
只是说这就是我解决这个问题的方法..我使用了非常好的 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>
Run Code Online (Sandbox Code Playgroud)
我还没有尝试过 jackson-module-jsonSchema 2.5.0+,因为上面的内容满足了我的需要。感谢大家的帮助。
| 归档时间: |
|
| 查看次数: |
9851 次 |
| 最近记录: |