英文:
@GetMapping return string info that list is empty
问题
有时可能会发生这种情况,在数据库中没有任何内容,方法 .findAll() 没有任何内容可显示,并返回空的主体。我知道这个函数的返回类型是 "List
类注释是
@RestController
@RequestMapping(path = "/users")
代码示例:
@GetMapping
public Iterable<User> findAll() {
List<User> userList = userRepository.findAll();
if(userList.isEmpty()){
// return "This list is empty :(";
}
return userList;
}
英文:
Sometimes there may happen, that in database is nothing, method .findAll() has nothing to show and returns empty body. I know I have return type of this function "List<User>" and I can not directly return string, but how could I send respond body as string or JSON, if list is empty, to let user know? I want to give receiver information that database is empty so it is clear for him.
Class annotations are
@RestController
@RequestMapping(path = "/users")
Code example:
@GetMapping
public Iterable<User> findAll() {
List<User> userList = userRepository.findAll();
if(userList.isEmpty()){
// return "This list is empty :(";
}
return userList;
}
答案1
得分: 1
这在前端部分可能是你会做的事情之一。然而,你可以返回一个包含列表和表示列表信息的字符串的 POJO。
class UserFindAllResponse {
private final List<User> users;
private final String message;
// 构造函数,获取方法
}
@GetMapping
public UserFindAllResponse findAll() {
List<User> userList = userRepository.findAll();
return new UserFindAllResponse(userList, userList.isEmpty() ? "似乎没有用户" : "有 x 个用户");
}
英文:
This is more or less something you might do on the front-end. However, what you could return is a POJO that contains the List and a String representing a message about the List, if necessary.
class UserFindAllResponse {
private final List<User> users;
private final String message;
// constructor, getters
}
@GetMapping
public UserFindAllResponse findAll() {
List<User> userList = userRepository.findAll();
return new UserFindAllResponse(userList, userList.isEmpty() ? "There appears to be no users" : "There are x users");
}
答案2
得分: 1
在你的控制层中,你应该返回一个包含从数据库返回的列表的响应映射。因此,如果列表为空,前端上的值将为零。
因此,如果userList
的长度为空,我们就知道数据库是空的,你可以向用户显示一条消息。
前端伪代码示例
fetch("${url}/users").then(response => {
if (response.data.length == 0) {
# 在这里显示消息,可以选择任何你喜欢的方式
alert("哦不!数据库表是空的")
}
else {
setData(response.data);
}
});
或者,你可以选择从后端抛出一个错误,然后在前端解决这个错误,并再次向用户显示有关空数据库的消息。
希望这能帮助你。
英文:
In your controller layer, you should return a response mapping with the returned list from the database. Therefore, if it is empty, the value on the front-end will be zero.
So if the length of the userList
is empty, we know the DB is empty and you can show a message to the user.
Example front-end Pseudo-code
fetch("${url}/users").then(response => {
if (response.data.length == 0) {
# Show message here, choose whichever way you want
alert("Oh no! Database table was empty")
}
else {
setData(response.data);
}
});
Alternatively, you could choose to throw an error from the back-end and then resolve the error on the front-end and again show a message to the user about the empty DB.
I hope this helps you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论