@GetMapping 返回字符串信息,指示列表为空。

huangapple go评论77阅读模式
英文:

@GetMapping return string info that list is empty

问题

有时可能会发生这种情况,在数据库中没有任何内容,方法 .findAll() 没有任何内容可显示,并返回空的主体。我知道这个函数的返回类型是 "List",我不能直接返回字符串,但如果列表为空,我怎么样才能将响应主体作为字符串或 JSON 发送,以让用户知道呢?我想要向接收者提供数据库为空的信息,以便他清楚地了解。

类注释是

@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 = &quot;/users&quot;)

Code example:

@GetMapping
public Iterable&lt;User&gt; findAll() {
    List&lt;User&gt; userList = userRepository.findAll();
    if(userList.isEmpty()){
        // return &quot;This list is empty :(&quot;;
    }
    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&lt;User&gt; users;

    private final String message;

    // constructor, getters

}
@GetMapping
public UserFindAllResponse findAll() {
    List&lt;User&gt; userList = userRepository.findAll();

    return new UserFindAllResponse(userList, userList.isEmpty() ? &quot;There appears to be no users&quot; : &quot;There are x users&quot;);
}

答案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(&quot;${url}/users&quot;).then(response =&gt; {
    if (response.data.length == 0) {
        # Show message here, choose whichever way you want
        alert(&quot;Oh no! Database table was empty&quot;)
    }
    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.

huangapple
  • 本文由 发表于 2020年4月4日 05:06:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61020411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定