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;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
public int id;

View File

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