|
1 | 1 | package org.example; |
2 | 2 |
|
| 3 | +import io.avaje.http.api.*; |
| 4 | +import io.helidon.common.http.HttpMediaType; |
| 5 | +import io.helidon.nima.webserver.http.ServerRequest; |
| 6 | +import io.helidon.nima.webserver.http.ServerResponse; |
| 7 | + |
3 | 8 | import java.util.List; |
4 | 9 | import java.util.Map; |
5 | 10 | import java.util.Set; |
6 | 11 |
|
7 | | -import io.avaje.http.api.Controller; |
8 | | -import io.avaje.http.api.Form; |
9 | | -import io.avaje.http.api.Get; |
10 | | -import io.avaje.http.api.Post; |
11 | | -import io.avaje.http.api.Produces; |
12 | | -import io.avaje.http.api.Put; |
13 | | -import io.helidon.nima.webserver.http.ServerRequest; |
14 | | -import io.helidon.nima.webserver.http.ServerResponse; |
15 | | - |
16 | 12 | @Controller |
17 | 13 | public class HelloController { |
18 | 14 |
|
| 15 | + @Produces(MediaType.TEXT_PLAIN) |
| 16 | + @Get |
| 17 | + String index() { |
| 18 | + return "Hello world - index"; |
| 19 | + } |
| 20 | + |
| 21 | + @Produces(MediaType.TEXT_PLAIN) |
| 22 | + @Get("hello") |
| 23 | + String helloWorld() { |
| 24 | + return "Hello world"; |
| 25 | + } |
| 26 | + |
19 | 27 | @Produces("image/png") |
20 | 28 | @Get("/get") |
21 | 29 | byte[] testBytes() { |
22 | | - |
23 | 30 | return "not really an image but ok".getBytes(); |
24 | 31 | } |
25 | 32 |
|
26 | | - @Get("/void") |
27 | | - void testVoid(Person p, ServerResponse res) { |
28 | | - res.send("success"); |
29 | | - } |
30 | | - |
31 | 33 | @Get("/helidon") |
32 | 34 | void testHelidon(ServerRequest req, ServerResponse res) { |
| 35 | + res.headers().contentType(HttpMediaType.TEXT_PLAIN); |
| 36 | + res.send("success path:" + req.path()); |
| 37 | + } |
33 | 38 |
|
34 | | - res.send("success"); |
| 39 | + @Get("/void") |
| 40 | + void testVoid(ServerResponse res) { |
| 41 | + res.send("GET-Returning-void"); |
35 | 42 | } |
36 | 43 |
|
37 | | - @Get("hello") |
38 | | - String helloWorld() { |
39 | | - return "Hello world"; |
| 44 | + // curl -v localhost:8081/person/jack |
| 45 | + @Get("person/{name}") |
| 46 | + Person person(String name) { |
| 47 | + return new Person(42, name + " hello"); |
40 | 48 | } |
41 | 49 |
|
42 | | - @Get("person/{name}/{sortBy}") |
43 | | - Person person(String name, String sortBy) { |
44 | | - final var p = new Person(); |
45 | | - p.setId(42); |
46 | | - p.setName(name + " hello" + " sortBy:" + sortBy); |
47 | | - return p; |
| 50 | + // curl -X POST http://localhost:8081/person -H 'Content-Type: application/json' -d '{"id":942,"name":"Jimmy"}' |
| 51 | + @Post("/person") |
| 52 | + Person postPerson(Person body) { |
| 53 | + return new Person(42, "Returning " + body.name()); |
48 | 54 | } |
49 | 55 |
|
| 56 | + // curl -v localhost:8081/person/foo/list |
50 | 57 | @Get("person/{sortBy}/list") |
51 | 58 | List<Person> personList(String sortBy) { |
52 | | - final var p = new Person(); |
53 | | - p.setId(42); |
54 | | - return List.of(p, p); |
| 59 | + return List.of(new Person(42, "fooList"), new Person(43, "barList")); |
55 | 60 | } |
56 | 61 |
|
| 62 | + // curl -v localhost:8081/person/foo/set |
57 | 63 | @Get("person/{sortBy}/set") |
58 | 64 | Set<Person> personSet(String sortBy) { |
59 | | - final var p = new Person(); |
60 | | - p.setId(42); |
61 | | - return Set.of(p, p); |
| 65 | + return Set.of(new Person(42, "fooSet"), new Person(43, "barSet")); |
62 | 66 | } |
63 | 67 |
|
64 | 68 | @Get("person/{sortBy}/map") |
65 | 69 | Map<String, Person> personMap(String sortBy) { |
66 | | - final var p = new Person(); |
67 | | - p.setId(42); |
68 | | - return Map.of(sortBy, p); |
| 70 | + return Map.of("one", new Person(42, "fooMap"), "two", new Person(43, "barMap")); |
69 | 71 | } |
70 | 72 |
|
71 | 73 | @Post("person/update") |
72 | 74 | String add(Person newGuy) { |
73 | | - |
74 | | - return "New Guy Added"; |
| 75 | + return "New Guy Added - " + newGuy; |
75 | 76 | } |
76 | 77 |
|
77 | 78 | @Put("person/update") |
78 | 79 | String addMultiple(List<Person> newGuys) { |
79 | 80 | return "New Guys Added"; |
80 | 81 | } |
| 82 | + |
| 83 | + // curl -X POST http://localhost:8081/form -H "Content-Type: application/x-www-form-urlencoded" -d "name=Jimmy&email=jim@foo&url=notaurl" |
81 | 84 | @Form |
82 | 85 | @Post("form") |
83 | 86 | String form(String name, String email, String url) { |
84 | | - return name; |
| 87 | + return name + "-" + email + "-" + url; |
85 | 88 | } |
86 | 89 |
|
87 | | - @Form |
| 90 | + // curl -X POST http://localhost:8081/formBean -H "Content-Type: application/x-www-form-urlencoded" -d "name=FormBeanJimmy&email=jim@foo&url=notaurl" |
| 91 | + @Form |
88 | 92 | @Post("formBean") |
89 | 93 | String formBean(MyForm form) { |
90 | | - return form.email; |
| 94 | + return form.name + "-" + form.email + "-" + form.url; |
91 | 95 | } |
92 | 96 |
|
93 | 97 | } |
0 commit comments