@@ -25,35 +25,37 @@ You define the api meta data in classes' static code blocks, then it was collect
25
25
#### 0) add the dependency to your project:
26
26
``` xml
27
27
<dependency >
28
- <groupId >com.github.tminglei</groupId >
29
- <artifactId >binder-swagger-java</artifactId >
30
- <version >0.8.0</version >
28
+ <groupId >com.github.tminglei</groupId >
29
+ <artifactId >binder-swagger-java</artifactId >
30
+ <version >0.8.0</version >
31
31
</dependency >
32
32
```
33
33
#### 1) define and register your api operations:
34
34
``` java
35
35
// in `PetResource.java`
36
36
static Mapping<?> petStatus = $(text(oneOf(Arrays . asList(" available" , " pending" , " sold" ))))
37
- .desc(" pet status in the store" ). $$;
37
+ .desc(" pet status in the store" ). example( " available " ) . $$;
38
38
static Mapping<?> pet = $(mapping(
39
- field(" id" , $(vLong()). desc(" pet id" ). $$),
39
+ field(" id" , $(vLong()). desc(" pet id" ). example(gen( " petId " ) . or(gen(() - > new Faker () . number() . randomNumber()))) . $$),
40
40
field(" name" , $(text(required())). desc(" pet name" ). $$),
41
41
field(" category" , attach(required()). to($(mapping(
42
- field(" id" , vLong(required())),
43
- field(" name" , text(required()))
42
+ field(" id" , vLong(required())),
43
+ field(" name" , text(required()))
44
44
)). refName(" category" ). desc(" category belonged to" ). $$)),
45
- field(" photoUrls" , $(list(text())). desc(" pet's photo urls" ). $$),
46
- field(" tags" , $(list(text())). desc(" tags for the pet" ). $$),
45
+ field(" photoUrls" , $(list(text())). desc(" pet's photo urls" ). example( Arrays . asList( " http://example.com/photo1 " )) . $$),
46
+ field(" tags" , $(list(text())). desc(" tags for the pet" ). example( Arrays . asList( " tag1 " , " tag2 " )) . $$),
47
47
field(" status" , petStatus)
48
48
)). refName(" pet" ). desc(" pet info" ). $$;
49
49
50
+ static SharingHolder sharing = sharing(). pathPrefix(" /pet" ). tag(" pet" );
51
+
50
52
static {
51
- operation(" post " , " /pet " )
52
- .summary(" create a pet" )
53
- .tag( " pet " )
54
- .parameter(param (pet) . in( " body " ))
55
- .response(200 , response(). description(" success " ))
56
- .response( 400 , response())
53
+ sharing . operation(GET , " /:petId<[0-9]+> " )
54
+ .summary(" get pet by id " )
55
+ .parameter(param(longv()) . in( " path " ) . name( " petId " ) . example( 1l ) )
56
+ .response( 200 , response (pet))
57
+ .response(404 , response(). description(" pet not found " ))
58
+ .notImplemented() // MARK IT `notImplemented`, THEN `binder-swagger-java` WILL GENERATE MOCK RESPONSE FOR YOU
57
59
;
58
60
}
59
61
@POST
@@ -64,31 +66,31 @@ public Response addPet(String data) throws BadRequestException, SQLException {
64
66
```java
65
67
// in `Bootstrap.java`
66
68
static { // for swagger
67
- swagger(). info(info()
68
- .title(" Swagger Sample App" )
69
- .description(" This is a sample server Petstore server. You can find out more about Swagger " +
70
- " at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, " +
71
- " you can use the api key `special-key` to test the authorization filters." )
72
- .termsOfService(" http://swagger.io/terms/" )
73
- .contact(contact(). email(" apiteam@swagger.io" ))
74
- .license(license(). name(" Apache 2.0" )
75
- .url(" http://www.apache.org/licenses/LICENSE-2.0.html" )
76
- )
77
- ). host(" localhost:8002" )
78
- .basePath(" /api" )
79
- .consumes(" application/json" )
80
- .produces(" application/json" )
81
- .securityDefinition(" api_key" , apiKeyAuth(" api_key" , In . HEADER ))
82
- .securityDefinition(" petstore_auth" , oAuth2()
83
- .implicit(" http://petstore.swagger.io/api/oauth/dialog" )
84
- .scope(" read:pets" , " read your pets" )
85
- .scope(" write:pets" , " modify pets in your account" )
86
- ). tag(tag(" pet" ). description(" Everything about your Pets" )
87
- .externalDocs(externalDocs(). description(" Find out more" ). url(" http://swagger.io" ))
88
- ). tag(tag(" store" ). description(" Access to Petstore orders" )
89
- ). tag(tag(" user" ). description(" Operations about user" )
90
- .externalDocs(externalDocs(). description(" Find out more about our store" ). url(" http://swagger.io" ))
91
- );
69
+ swagger(). info(info()
70
+ .title(" Swagger Sample App" )
71
+ .description(" This is a sample server Petstore server. You can find out more about Swagger " +
72
+ " at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, " +
73
+ " you can use the api key `special-key` to test the authorization filters." )
74
+ .termsOfService(" http://swagger.io/terms/" )
75
+ .contact(contact(). email(" apiteam@swagger.io" ))
76
+ .license(license(). name(" Apache 2.0" )
77
+ .url(" http://www.apache.org/licenses/LICENSE-2.0.html" )
78
+ )
79
+ ). host(" localhost:8002" )
80
+ .basePath(" /api" )
81
+ .consumes(" application/json" )
82
+ .produces(" application/json" )
83
+ .securityDefinition(" api_key" , apiKeyAuth(" api_key" , In . HEADER ))
84
+ .securityDefinition(" petstore_auth" , oAuth2()
85
+ .implicit(" http://petstore.swagger.io/api/oauth/dialog" )
86
+ .scope(" read:pets" , " read your pets" )
87
+ .scope(" write:pets" , " modify pets in your account" )
88
+ ). tag(tag(" pet" ). description(" Everything about your Pets" )
89
+ .externalDocs(externalDocs(). description(" Find out more" ). url(" http://swagger.io" ))
90
+ ). tag(tag(" store" ). description(" Access to Petstore orders" )
91
+ ). tag(tag(" user" ). description(" Operations about user" )
92
+ .externalDocs(externalDocs(). description(" Find out more about our store" ). url(" http://swagger.io" ))
93
+ );
92
94
}
93
95
```
94
96
#### 3 ) configure the filter, which will serv the `swagger. json`:
0 commit comments