SpringBoot简单问题我找不到解决方案如何解决这个问题

时间:2020-11-11 14:13:05

标签: java spring-boot

如何使用ArrayList返回ResponseEntity

代码:

List<String> userName=new ArrayList<>();

    @GetMapping("/users")
    public @ResponseBody List<userName> getAllHospitals() throws Exception {
        return new ResponseEntity<userName>(userName,HttpStatus.OK);
    }

1 个答案:

答案 0 :(得分:0)

首先,如果“ userName”是您的类的名称,则将其重命名为 Username ,因为这是使类名以大写字母开头的良好Java约定。

第二,您需要正确命名该方法,并准确描述其作用。因此,如果您正在构建一种通过地图获取所有用户的方法,则该方法应该是“ getAllUsers”,而不是“ getAllHospitals”

如果您需要通过映射方法返回列表,则需要使用适当的通用形式:

    @GetMapping("/users")
    public @ResponseBody ResponseEntity<List<Username>> getAllUsers() throws Exception {
        List<Username> usernames = new ArrayList<>();
        usernames.add(new Username(....)); // substitute with parameters of that class
        return new ResponseEntity<List<Username>>(usernames,HttpStatus.OK);
    }
相关问题