Skip to content

Instantly share code, notes, and snippets.

@rhamedy
Created September 27, 2020 01:58
Show Gist options
  • Save rhamedy/55b37b1fba723d428c27409b61c4176d to your computer and use it in GitHub Desktop.
Save rhamedy/55b37b1fba723d428c27409b61c4176d to your computer and use it in GitHub Desktop.
A Generic validator for getById type of methods from Spring Data
package com.sampleservice.demo.validator;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
import java.util.Optional;
@Component
public class StudentValidator {
public <T> void validate404(Optional<T> object, String label, String value) {
if (!object.isPresent()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
object.getClass().getName() + " with " + label + "'" + value + "' does not exist.", null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment