"no result exception" for Spring @Service's

yaseco :

I have multiple @Service's in my Spring boot application which mainly make API calls to some external services.

Do Spring offers a custom Exception for a kind of a "404" exception? i.e. the item not found / there's no result for the requested key.

I'm aware of NoSuchElementException class but it doesn't seem to really fit here.

I guess I could create my own NoResultException but I'd like to follow the convention (if there's such)

Thanks!

James :

There is no such exception provided by the Spring framework for the case of 404, or any other HttpStatus, for that matter.

You should ideally handle that in your service layer based on the API response and throw a custom exception which denotes the 404 case according to your domain. You can have a @ControllerAdvice which handles the specific exception and provides a custom response accordingly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=146799&siteId=1