JJ *_*kar 26 java validation swagger
我正在使用Swagger(1.5.8).我希望我的swagger.json
定义能够在我的DTO上检测javax.validation
JSR-303注释,以便我可以记录我的API的验证约束.
我希望@Min
注释(如本例)会显示最小值(44),但事实并非如此.
@POST
@ApiOperation(value = "post", httpMethod = "POST")
public Response post(
@QueryParam("id") @NotNull @Min(44) Integer id) {...}
Run Code Online (Sandbox Code Playgroud)
结果swagger.json
是:
"/foo": {
"post": {
"operationId": "post",
...
"parameters": [
{
"in": "body",
"name": "id",
"description": "id",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
Run Code Online (Sandbox Code Playgroud)
Swagger 关闭了此功能的pull请求,但我不清楚它在Swagger定义中的使用位置和方式.
我希望能够做到这样的事情:
@POST
public void postFoo(@Valid @RequestBody FooDTO fooDto) {...}
Run Code Online (Sandbox Code Playgroud)
public class FooDTO {
@NotNull
@Size(min = 1, max = 100)
private Integer myInt;
}
Run Code Online (Sandbox Code Playgroud)
"FooDTO": {
"type": "object",
"required": [
"myInt"
],
"properties": {
"myInt": {
"type": "number",
"format": "integer",
"minimum": "1",
"maximum": "100",
...
Run Code Online (Sandbox Code Playgroud)
什么是配置扬鞭模块/插件,使之类的首选方式ModelResolver
,并BeanValidator
,这样他们就会检查我的DTO的注释?
最新的,截至目前,Swagger-Core 版本 1.5.19 完美支持这一点:
DTO 对象与此类似:
public class SampleDTO {
@Min(value = 5)
@Max(value = 10)
@NotNull
private Integer integer;
@NotNull
private String string;
//...
}
Run Code Online (Sandbox Code Playgroud)
将生成类似于此的swagger.json :
...
"definitions" : {
"SampleDTO" : {
"type" : "object",
"required" : [ "integer", "string" ],
"properties" : {
"integer" : {
"type" : "integer",
"format" : "int32",
"minimum" : 5,
"maximum" : 10
},
"string" : {
"type" : "string"
},
...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4609 次 |
最近记录: |