Merge pull request #12489 from panos-kakos/JAVA-12943

[JAVA-12943] Improvement
This commit is contained in:
kwoyke 2022-07-14 09:17:28 +02:00 committed by GitHub
commit f98efe9079
2 changed files with 6 additions and 5 deletions

View File

@ -1,5 +1,5 @@
package com.baeldung.javalin.User; package com.baeldung.javalin.User;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User { public class User {
public int id; public int id;

View File

@ -3,6 +3,7 @@ package com.baeldung.javalin.User;
import io.javalin.Handler; import io.javalin.Handler;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
public class UserController { public class UserController {
public static Handler fetchAllUsernames = ctx -> { public static Handler fetchAllUsernames = ctx -> {
@ -14,11 +15,11 @@ public class UserController {
public static Handler fetchById = ctx -> { public static Handler fetchById = ctx -> {
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id"))); int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
UserDao dao = UserDao.instance(); UserDao dao = UserDao.instance();
User user = dao.getUserById(id).get(); Optional<User> user = dao.getUserById(id);
if (user == null) { if(user.isPresent()){
ctx.html("Not Found");
} else {
ctx.json(user); ctx.json(user);
} else {
ctx.html("User Not Found");
} }
}; };
} }