Skip to content

Commit bf2515c

Browse files
authored
Update README.md
1 parent 97ae018 commit bf2515c

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

README.md

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,37 @@ You define the api meta data in classes' static code blocks, then it was collect
2525
#### 0) add the dependency to your project:
2626
```xml
2727
<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>
3131
</dependency>
3232
```
3333
#### 1) define and register your api operations:
3434
```java
3535
// in `PetResource.java`
3636
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").$$;
3838
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()))).$$),
4040
field("name", $(text(required())).desc("pet name").$$),
4141
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()))
4444
)).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")).$$),
4747
field("status", petStatus)
4848
)).refName("pet").desc("pet info").$$;
4949

50+
static SharingHolder sharing = sharing().pathPrefix("/pet").tag("pet");
51+
5052
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
5759
;
5860
}
5961
@POST
@@ -64,31 +66,31 @@ public Response addPet(String data) throws BadRequestException, SQLException {
6466
```java
6567
// in `Bootstrap.java`
6668
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+
);
9294
}
9395
```
9496
#### 3) configure the filter, which will serv the `swagger.json`:

0 commit comments

Comments
 (0)