This repository was archived by the owner on Mar 3, 2026. It is now read-only.
forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppWithDoc.java
More file actions
98 lines (84 loc) · 2.21 KB
/
AppWithDoc.java
File metadata and controls
98 lines (84 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package apps;
import org.jooby.Err;
import org.jooby.Jooby;
import org.jooby.Status;
import org.jooby.apitool.ApiTool;
import java.util.List;
public class AppWithDoc extends Jooby {
private static final int START = 0;
private static final int MAX = 200;
{
/**
* Home page.
*/
get("/", () -> "Hi");
/**
* Everything about your Pets.
*/
path("/api/pets", () -> {
/**
* Find pet by ID.
*
* @param id Pet ID.
* @return Returns <code>200</code> with a single pet or <code>404</code>
*/
get("/:id", req -> {
int id = req.param("id").intValue();
DB db = req.require(DB.class);
Pet result = db.find(id);
if (result == null) {
throw new Err(Status.NOT_FOUND);
}
return result;
});
/**
*
* List pets ordered by id.
*
* @param start Start offset, useful for paging. Default is <code>0</code>.
* @param max Max page size, useful for paging. Default is <code>50</code>.
* @return Pets ordered by name.
*/
get(req -> {
int start = req.param("start").intValue(START);
int max = req.param("max").intValue(MAX);
DB db = req.require(DB.class);
List<Pet> results = db.findAll(start, max);
return results;
});
/**
*
* Add a new pet to the store.
*
* @param body Pet object that needs to be added to the store.
* @return Returns a saved pet.
*/
post(req -> {
Pet body = req.body(Pet.class);
DB db = req.require(DB.class);
body = db.create(body);
return body;
});
/**
*
* Deletes a pet by ID.
*
* @param id Pet ID.
* @return A <code>204</code>
*/
delete("/:id", req -> {
int id = req.param("id").intValue();
DB db = req.require(DB.class);
Pet result = db.delete(id);
return result;
});
});
use(new ApiTool()
.filter(r -> r.pattern().startsWith("/api/"))
.raml("/raml")
.swagger("/swagger"));
}
public static void main(String[] args) throws Exception {
run(AppWithDoc::new, args);
}
}