-
Notifications
You must be signed in to change notification settings - Fork 141
RestController中设置返回的ResponseCode
gexiangdong edited this page Jun 1, 2018
·
2 revisions
可以把方法设置成返回ResponseEntity类型,其中的泛型T取代以前想返回的类型,然后通过ResponseEntity.status(HttpStatus.OK).body(result)方法创建返回结果,其中第一个status就是设置返回的ResponseCode
@PostMapping
public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> userInfo){
String userName = userInfo.get("username");
String password = userInfo.get("password");
HashMap<String, Object> result = new HashMap<>();
String token = tokenService.login(userName, password);
if(token == null) {
result.put("message", "invalid username or password");
// status(HttpStatus.BAD_REQUEST) 是设置返回的状态码
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}else {
result.put("token", token);
return ResponseEntity.status(HttpStatus.OK).body(result);
}
}