I am calling restTemplate.getForObject to retrieve a certain value from Mongo DB. How to deal the exception if the expected data is not found in the DB?
Object[] mongodata = restTemplate.getForObject(resulturl,Object[].class,keyval);
list = Arrays.asList(mongodata);
where keyval is a string that contains a json and resulturl is the url for calling mongo
Basically, you have two main options:
RestTemplatecall in atry-catchblock and handle the error (in case of 404 not found response, it would be theHttpClientErrorException). Something likeResponseErrorHandler.See this post on Baeldung for an example.