Chcę podmienić komunikat wyjątku, gdy próbuję dobrać się do obiektu, którego nie ma.
Mam tak o:
public class ResourceNotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public ResourceNotFoundException() {
}
public ResourceNotFoundException(String message) {
super(message);
}
public ResourceNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public ResourceNotFoundException(Throwable cause) {
super(cause);
}
public ResourceNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
.
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({ ResourceNotFoundException.class })
public ResponseEntity<Object> handleNotFoundException(Exception exc, WebRequest req) {
return new ResponseEntity<Object>("*************** TROLOLOLOLO **********************", new HttpHeaders(), HttpStatus.NOT_FOUND);
}
}
.
@Service
public class CustomerServiceImpl implements CustomerService {
private final CustomerRepository customerRepo;
private final CustomerMapper customerMapper;
@Autowired
public CustomerServiceImpl(CustomerRepository repository, CustomerMapper mapper) {
this.customerRepo = repository;
this.customerMapper = mapper;
}
@Override
public CustomerDto getCustomerByName(String name) {
CustomerDto dto = customerMapper.customerToCustomerDto(customerRepo.findByName(name));
if (dto == null) {
System.out.println("ups ******************************");
throw new ResourceNotFoundException();
}
return dto;
}
@Override
public CustomerDto patchCustomer(Long id, CustomerDto customerDto) {
return customerRepo.findById(id).map(customer -> {
if (customerDto.getName() != null) {
customer.setName(customerDto.getName());
}
CustomerDto returnDto = customerMapper.customerToCustomerDto(customerRepo.save(customer));
returnDto.setCustomerUrl(getCustomerUrl(returnDto.getName()));
return returnDto;
}).orElseThrow(ResourceNotFoundException::new);
}
}
.
public interface CustomerRepository extends JpaRepository<Customer, Long> {
Customer findByName(String name);
}
a mimo to wciąż jak próbuję np. wyszukać nie istniejący rekord to nie dostaję komunikatu *************** TROLOLOLOLO **********************
ale ****** ups
się wyświetla w konsoli.