Merge pull request #12489 from panos-kakos/JAVA-12943
[JAVA-12943] Improvement
This commit is contained in:
commit
f98efe9079
|
@ -1,5 +1,5 @@
|
|||
package com.baeldung.javalin.User;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
|
||||
public class User {
|
||||
public int id;
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue