Обработване на изключения в Spring Boot приложение
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception1.class)
public String handleException1() {
// handles exception type Exception1
}
@ExceptionHandler(Exception2.class)
public String handleException2() {
// handles exception type Exception2
}
// handlers for other exceptions...
}
@Service
public class CityService implements ICityService {
private final CityRepository cityRepository;
public CityService(CityRepository cityRepository) {
this.cityRepository = cityRepository;
}
@Override
public City findById(Long id) {
return cityRepository.findById(id)
.orElseThrow(() -> new CityNotFoundException(id));
}
}Last updated