[JAVA-12943] Improvement

This commit is contained in:
panagiotiskakos 2022-07-14 08:23:11 +03:00
parent 56fd1ef7b4
commit e80218d1e0
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");
} }
}; };
} }