我正在使用 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();
}
}