Spring 引导 ResponseEntity 不操作 HTTP 响应
Spring Boot ResponseEntity not Manupulating the HTTP Responses
我在响应实体异常处理方面遇到问题。正如所见,我的响应实体错误并未更改 HTTP 响应。
我的代码
public ResponseEntity<User> retriveUser(@PathVariable int id){
Optional<User> foundUser;
foundUser= userRepo.findById(id);
if(foundUser.get()==null) {
return new ResponseEntity<>(foundUser.get(),HttpStatus.HttpStatus.NOT_FOUND);
}
else {
return new ResponseEntity<>(foundUser.get(),HttpStatus.OK);
}
}
将您的代码更改为以下内容
if(foundUser.isPresent()) {
return new ResponseEntity<>(foundUser.get(),HttpStatus.OK);
} else {
return new ResponseEntity<>(foundUser.get(),HttpStatus.HTTP_VERSION_NOT_SUPPORTED);
}
您的代码中有一些错误,首先 foundUser.get()==null
部分没有进入 if 块,因为它抛出错误。您可以检查 java document 以找出引发错误的原因。
- 还需要
HttpStatus.NOT_FOUND
而不是HttpStatus.HttpStatus.NOT_FOUND
.
- 在“Not Found”这一行,这样
optionalUser.get()
方法就不会报错;你也必须删除它。
@GetMapping("/user/{id}")
public ResponseEntity<User> retrieveUser(@PathVariable int id) {
Optional<User> optionalUser = userRepo.findById(id);
if (!optionalUser.isPresent()) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(optionalUser.get(), HttpStatus.OK);
}
}
如果值不存在,您不应调用 get()
方法。你能像下面那样试试吗
public ResponseEntity<User> retriveUser(@PathVariable int id){
Optional<User> foundUser = userRepo.findById(id);
if(foundUser.isPresent()) {
return new ResponseEntity<>(foundUser.get(),HttpStatus.HttpStatus.OK);
}else {
return new ResponseEntity<>(null,HttpStatus.NOT_FOUND);
//or just return not found status code
//return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
虽然目前接受的答案已经包含了它需要的所有细节,但我已经在这里添加了我的建议,请看一看。
你可以去掉 if else
块
@RestController
@RequestMapping(path = "users")
static class UserController {
private final Map<Long, UserInfo> users = new HashMap<>();
UserController() {
users.put(1L, new UserInfo(1L, "User 1"));
users.put(2L, new UserInfo(1L, "User 2"));
}
@GetMapping(path = "{id}")
public ResponseEntity<UserInfo> get(@PathVariable("id") Long id) {
return findUserById(id)
.map(ResponseEntity::ok)
.orElse(new ResponseEntity<>(UserInfo.notFoundUser(), HttpStatus.NOT_FOUND));
}
Optional<UserInfo> findUserById(@NonNull Long id) {
return Optional.ofNullable(users.get(id));
}
static class UserInfo {
Long id;
String name;
public UserInfo() {
}
public UserInfo(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
static UserInfo notFoundUser() {
return new UserInfo(-1L, null);
}
}
}
我在响应实体异常处理方面遇到问题。正如所见,我的响应实体错误并未更改 HTTP 响应。
我的代码
public ResponseEntity<User> retriveUser(@PathVariable int id){
Optional<User> foundUser;
foundUser= userRepo.findById(id);
if(foundUser.get()==null) {
return new ResponseEntity<>(foundUser.get(),HttpStatus.HttpStatus.NOT_FOUND);
}
else {
return new ResponseEntity<>(foundUser.get(),HttpStatus.OK);
}
}
将您的代码更改为以下内容
if(foundUser.isPresent()) {
return new ResponseEntity<>(foundUser.get(),HttpStatus.OK);
} else {
return new ResponseEntity<>(foundUser.get(),HttpStatus.HTTP_VERSION_NOT_SUPPORTED);
}
您的代码中有一些错误,首先 foundUser.get()==null
部分没有进入 if 块,因为它抛出错误。您可以检查 java document 以找出引发错误的原因。
- 还需要
HttpStatus.NOT_FOUND
而不是HttpStatus.HttpStatus.NOT_FOUND
. - 在“Not Found”这一行,这样
optionalUser.get()
方法就不会报错;你也必须删除它。
@GetMapping("/user/{id}")
public ResponseEntity<User> retrieveUser(@PathVariable int id) {
Optional<User> optionalUser = userRepo.findById(id);
if (!optionalUser.isPresent()) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(optionalUser.get(), HttpStatus.OK);
}
}
如果值不存在,您不应调用 get()
方法。你能像下面那样试试吗
public ResponseEntity<User> retriveUser(@PathVariable int id){
Optional<User> foundUser = userRepo.findById(id);
if(foundUser.isPresent()) {
return new ResponseEntity<>(foundUser.get(),HttpStatus.HttpStatus.OK);
}else {
return new ResponseEntity<>(null,HttpStatus.NOT_FOUND);
//or just return not found status code
//return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
虽然目前接受的答案已经包含了它需要的所有细节,但我已经在这里添加了我的建议,请看一看。
你可以去掉 if else
块
@RestController
@RequestMapping(path = "users")
static class UserController {
private final Map<Long, UserInfo> users = new HashMap<>();
UserController() {
users.put(1L, new UserInfo(1L, "User 1"));
users.put(2L, new UserInfo(1L, "User 2"));
}
@GetMapping(path = "{id}")
public ResponseEntity<UserInfo> get(@PathVariable("id") Long id) {
return findUserById(id)
.map(ResponseEntity::ok)
.orElse(new ResponseEntity<>(UserInfo.notFoundUser(), HttpStatus.NOT_FOUND));
}
Optional<UserInfo> findUserById(@NonNull Long id) {
return Optional.ofNullable(users.get(id));
}
static class UserInfo {
Long id;
String name;
public UserInfo() {
}
public UserInfo(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
static UserInfo notFoundUser() {
return new UserInfo(-1L, null);
}
}
}