I would like to through an exception in spring boot that is descriptive and has a reason included ie. "Dog not found". At the moment I get the exception but it is generic:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Apr 20 09:32:07 CEST 2021
There was an unexpected error (type=Not Found, status=404).
The error is thrown from the @Service method implementation:
@Service
public class DogServiceImpl implements DogService {
@Autowired
DogRepository dogRepository;
public String retrieveDogBreedById(Long id){
Optional<String> optionalBreed = Optional.ofNullable(dogRepository.findBreedById(id));
String breed = optionalBreed.orElseThrow(DogNotFoundException::new);
return breed;
};
And here is my exception:
@ResponseStatus(value= HttpStatus.NOT_FOUND, reason="Dog not found")
public class DogNotFoundException extends RuntimeException{
public DogNotFoundException() {
}
public DogNotFoundException(String message) {
super(message);
}
}
I am using @RestController for a REST API