[英]How to handle exception thrown from ExceptionHandler in controller with ExceptionHandler in ControllerAdvice?
[英]@ExceptionHandler not getting called once exception is thrown from REST layer
我正在使用Spring MVC開發REST服務。 我正在嘗試使用@ExceptionHandler實現異常處理。 從REST層拋出異常時,@ ExceptionHandler不會攔截該異常。 我有什么想念的嗎?
@Service
@Path("/customer")
public class CustomerResource extends BaseResource{
@Autowired
private CustomerDao customerDao;
........
@GET
@Path("/customer/{accountNumber}")
public Response findCustomerByAccountNumber(String accountNumber) throw Exception{
Customer customer=null;
customer=customerDao.find(....);
if(customer==null)
throw new ResourceNotFoundException();
else
..........
}
}
具有異常處理程序方法的基類
public abstract class BaseResource {
.......
@ExceptionHandler({ResourceNotFoundException.class })
public Response handleException(Exception ex) {
ErrorResource errResource = new ErrorResource();
.....
return Response.status(Response.Status.NOT_FOUND).entity(errResource).build();
}
}
您將拋出ResourceNotFound,但已在異常處理程序中指定了ResourceNotFoundException-這些似乎是不同的異常。 拋出ResourceNotFoundException而不是ResourceNotFound或將ResourceNotFound添加到異常處理程序。
編輯:不知道我一開始怎么想的:只是注意到您實際上並沒有使用Spring MVC控制器。 Spring MVC異常處理程序僅適用於由Spring MVC控制器處理的請求。 它們處理發生在控制器處理程序方法主體中的異常。 您似乎還使用其他方法來處理REST請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.