Skip to content

Commit ca5a7d2

Browse files
christopherfranklinpivovarit
authored andcommitted
[BAEL-1659] Create REST microservices with javalin (eugenp#4145)
* [BAEL-1615] Code to support article. * Adding example of provides...with * Adding uses to main module * [BAEL-1659] Creat REST microservices with javalin * [BAEL-1659] Update article code based on feedback * Revert parent pom change * Using Optional instead of null
1 parent 196223d commit ca5a7d2

5 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@
680680
<version>${unirest.version}</version>
681681
</dependency>
682682

683+
<!-- javalin -->
684+
<dependency>
685+
<groupId>io.javalin</groupId>
686+
<artifactId>javalin</artifactId>
687+
<version>1.6.0</version>
688+
</dependency>
683689
<!-- Atlassian Fugue -->
684690
<dependency>
685691
<groupId>io.atlassian.fugue</groupId>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.javalin;
2+
3+
import com.baeldung.javalin.User.UserController;
4+
import io.javalin.Javalin;
5+
6+
public class JavalinApp {
7+
public static void main(String[] args) {
8+
Javalin app = Javalin.create()
9+
.port(7000)
10+
.start();
11+
12+
app.get("/hello", ctx -> ctx.html("Hello, Javalin!"));
13+
app.get("/users", UserController.fetchAllUsernames);
14+
app.get("/users/:id", UserController.fetchById);
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.javalin.User;
2+
3+
public class User {
4+
public final int id;
5+
public final String name;
6+
7+
public User(int id, String name) {
8+
this.id = id;
9+
this.name = name;
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.javalin.User;
2+
3+
import io.javalin.Handler;
4+
5+
import java.util.Objects;
6+
7+
public class UserController {
8+
public static Handler fetchAllUsernames = ctx -> {
9+
UserDao dao = UserDao.instance();
10+
Iterable<String> allUsers = dao.getAllUsernames();
11+
ctx.json(allUsers);
12+
};
13+
14+
public static Handler fetchById = ctx -> {
15+
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
16+
UserDao dao = UserDao.instance();
17+
User user = dao.getUserById(id);
18+
if (user == null) {
19+
ctx.html("Not Found");
20+
} else {
21+
ctx.json(user);
22+
}
23+
};
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.javalin.User;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
import java.util.stream.Collectors;
7+
8+
class UserDao {
9+
10+
private final List<User> users = Arrays.asList(
11+
new User(0, "Steve Rogers"),
12+
new User(1, "Tony Stark"),
13+
new User(2, "Carol Danvers")
14+
);
15+
16+
private static UserDao userDao = null;
17+
18+
private UserDao() {
19+
}
20+
21+
static UserDao instance() {
22+
if (userDao == null) {
23+
userDao = new UserDao();
24+
}
25+
return userDao;
26+
}
27+
28+
Optional<User> getUserById(int id) { return users.stream().filter(u -> u.id == id).findFirst(); }
29+
30+
Iterable<String> getAllUsernames() {
31+
return users.stream().map(user -> user.name).collect(Collectors.toList());
32+
}
33+
}

0 commit comments

Comments
 (0)