我试图做的是找到带有id的todo,当它不存在时,它会抛出一个自定义异常。但是在 Postman 身上我看不到例外。什么都看不出来。
有人能告诉我我做错了什么吗?
ExceptionResponse.class
package com.lcwd.todo.exceptions;
import org.springframework.http.HttpStatus;
public class ExceptionResponse {
private String message;
private boolean success;
private HttpStatus status;
public ExceptionResponse(String message, boolean success, HttpStatus status) {
this.message = message;
this.success = success;
this.status = status;
}
public ExceptionResponse() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public HttpStatus getStatus() {
return status;
}
public void setStatus(HttpStatus status) {
this.status = status;
}
}
字符串
GlobalExceptionHandler.class
package com.lcwd.todo.exceptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
Logger logger= LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(NullPointerException.class)
public ResponseEntity<String> nullPointerException(NullPointerException ex){
System.out.println("This is null pointer exception");
logger.info("It is nullPointerException from GlobalExceptionHandler");
return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ExceptionResponse> resourceNotFoundExceptionHandler(ResourceNotFoundException ex){
ExceptionResponse response=new ExceptionResponse();
response.setMessage(ex.getMessage());
response.setMessage(ex.getMessage());
response.setSuccess(false);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}
}
型
ResourceNotFoundException.java
package com.lcwd.todo.exceptions;
import org.springframework.http.HttpStatus;
public class ResourceNotFoundException extends RuntimeException{
private String message;
private HttpStatus status;
public ResourceNotFoundException( String message, HttpStatus status) {
super(message);
this.message = message;
this.status = status;
}
public ResourceNotFoundException(){
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public HttpStatus getStatus() {
return status;
}
public void setStatus(HttpStatus status) {
this.status = status;
}
}
型
getToDoFunction
public ToDo getSingleToDo(int id){
ToDo todo=new ToDo();
for(ToDo todo1:todos){
if (todo1.getId() == id) {
return todo1;
}
todo=todo1;
}
if(todo==null){
throw new ResourceNotFoundException("Resource not found exception",HttpStatus.NOT_FOUND);
}
return null;
}
型
1条答案
按热度按时间fdx2calv1#
假设
todos
不为空,但不包含所需的id(包括todos
为空时):在循环中,不管id是否匹配,都要将todo
设置为每个对象的id,因此当循环结束时,除非找到匹配的id并提前返回,否则它将始终被设置为列表中的最后一项或初始的todo=new ToDo()
。因此它永远不会为null。但实际上,你真的需要这个对象吗?如果您删除它,并且在循环结束时总是抛出异常,会怎样?原因如果循环结束,则表示尚未找到资源。这也引出了一个问题,返回null和抛出ResourceNotFoundException之间的语义区别是什么?返回null实际上意味着什么?我想,如果异常指示未找到,您可能不需要返回null。