Java SpringBoot简单问题我不知道';我找不到解决办法怎么解决这个问题
如何使用Java SpringBoot简单问题我不知道';我找不到解决办法怎么解决这个问题,java,spring-boot,Java,Spring Boot,如何使用ResponseEntity 代码: List<String> userName=new ArrayList<>(); @GetMapping("/users") public @ResponseBody List<userName> getAllHospitals() throws Exception { return new ResponseEntity<userName>(user
ResponseEntity
代码:
List<String> userName=new ArrayList<>();
@GetMapping("/users")
public @ResponseBody List<userName> getAllHospitals() throws Exception {
return new ResponseEntity<userName>(userName,HttpStatus.OK);
}
List userName=new ArrayList();
@GetMapping(“/users”)
public@ResponseBody List getAllHospitals()引发异常{
返回新的响应属性(用户名,HttpStatus.OK);
}
首先,如果“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);
}
@GetMapping(“/users”)
public@ResponseBody ResponseEntity getAllUsers()引发异常{
List usernames=new ArrayList();
usernames.add(新用户名(..);//替换为该类的参数
返回新的响应属性(用户名,HttpStatus.OK);
}