我正试图在我的spring boot应用程序中集中错误处理。目前我只处理一个潜在的异常(nosuchelementexception),这是控制器建议:
import java.util.NoSuchElementException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(NoSuchElementException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public DispatchError dispatchNotFound(NoSuchElementException exception) {
System.out.println("asdasdasd");
return new DispatchError(exception.getMessage());
}
}
下面是抛出异常的服务:
import java.util.List;
import com.deliveryman.deliverymanapi.model.entities.Dispatch;
import com.deliveryman.deliverymanapi.model.repositories.DispatchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DaoService {
@Autowired
DispatchRepository dispatchRepo;
public Dispatch findByShipmentNumber(long shipmentNumber) {
return dispatchRepo.findById(shipmentNumber).orElseThrow();
}
public List<Dispatch> findByUser(String user, String status) {
if(status == null) {
return dispatchRepo.findByOriginator(user).orElseThrow();
} else {
return dispatchRepo.findByOriginatorAndStatus(user, status).orElseThrow();
}
}
public Dispatch createDispatch(Dispatch dispatch) { //TODO parameter null check exception
return dispatchRepo.save(dispatch);
}
}
问题是,一旦我发送了对不存在的资源的请求,所显示的json消息就是spring的默认消息。它应该是我自定义的json错误消息(dispatcherror)。
现在,可以通过向异常处理程序方法添加@responsebody来解决这个问题,但问题是我使用了我的一个旧代码作为引用,它在没有@responsebody注解的情况下可以正常工作。
有人能解释一下为什么会这样吗?
1条答案
按热度按时间pexxcrt21#
或者用
@ResponseBody
```@ControllerAdvice
@ResponseBody
public class ExceptionController {
...
因此,
@RestControllerAdvice
是的缩写来源单据
@ResponseBody
指示方法返回值的注解应绑定到web响应主体。支持带注解的处理程序方法。替代使用
@ControllerAdvice
仅限于:我对你的旧应用程序有个理论。根据您问题的建议和下面的错误处理程序,我可以创建一个
DispatchError
示例看起来是由advice返回的(advice被执行),但实际上是由error controller返回的。实施
ErrorController
进入类路径,替换spring的BasicErrorController
.加固时
@RestControllerAdvice
,错误控制器不再对有效NoSuchElementException
.在大多数情况下
ErrorController
处理所有错误的实现,结合针对更复杂异常(如MethodArgumentNotValidException
,应该足够了。这将需要像这样的一般错误数据为了
ErrorHandler
以上,更换handleError
用这个带验证异常处理的建议
application.yml中的相关配置
使用application.properties时
当使用springdatajpa时,请考虑使用以下设置来关闭第二次验证。
有关spring中异常处理的更多信息:
https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc (2018年4月修订)https://www.baeldung.com/exception-handling-for-rest-with-spring (2020年12月31日)