Skip to content

Commit 11e9730

Browse files
committed
test-nima-jsonb - Add @JSON annotation, Person and record type, tweak controller
1 parent 9051a1a commit 11e9730

File tree

2 files changed

+46
-57
lines changed

2 files changed

+46
-57
lines changed
Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,97 @@
11
package org.example;
22

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+
38
import java.util.List;
49
import java.util.Map;
510
import java.util.Set;
611

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-
1612
@Controller
1713
public class HelloController {
1814

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+
1927
@Produces("image/png")
2028
@Get("/get")
2129
byte[] testBytes() {
22-
2330
return "not really an image but ok".getBytes();
2431
}
2532

26-
@Get("/void")
27-
void testVoid(Person p, ServerResponse res) {
28-
res.send("success");
29-
}
30-
3133
@Get("/helidon")
3234
void testHelidon(ServerRequest req, ServerResponse res) {
35+
res.headers().contentType(HttpMediaType.TEXT_PLAIN);
36+
res.send("success path:" + req.path());
37+
}
3338

34-
res.send("success");
39+
@Get("/void")
40+
void testVoid(ServerResponse res) {
41+
res.send("GET-Returning-void");
3542
}
3643

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");
4048
}
4149

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());
4854
}
4955

56+
// curl -v localhost:8081/person/foo/list
5057
@Get("person/{sortBy}/list")
5158
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"));
5560
}
5661

62+
// curl -v localhost:8081/person/foo/set
5763
@Get("person/{sortBy}/set")
5864
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"));
6266
}
6367

6468
@Get("person/{sortBy}/map")
6569
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"));
6971
}
7072

7173
@Post("person/update")
7274
String add(Person newGuy) {
73-
74-
return "New Guy Added";
75+
return "New Guy Added - " + newGuy;
7576
}
7677

7778
@Put("person/update")
7879
String addMultiple(List<Person> newGuys) {
7980
return "New Guys Added";
8081
}
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"
8184
@Form
8285
@Post("form")
8386
String form(String name, String email, String url) {
84-
return name;
87+
return name + "-" + email + "-" + url;
8588
}
8689

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
8892
@Post("formBean")
8993
String formBean(MyForm form) {
90-
return form.email;
94+
return form.name + "-" + form.email + "-" + form.url;
9195
}
9296

9397
}
Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
package org.example;
22

3-
public class Person {
3+
import io.avaje.jsonb.Json;
44

5-
long id;
6-
String name;
5+
@Json
6+
public record Person(long id, String name) {
77

8-
public long getId() {
9-
return id;
10-
}
11-
12-
public void setId(long id) {
13-
this.id = id;
14-
}
15-
16-
public String getName() {
17-
return name;
18-
}
19-
20-
public void setName(String name) {
21-
this.name = name;
22-
}
238
}

0 commit comments

Comments
 (0)