Created
September 27, 2020 01:58
-
-
Save rhamedy/55b37b1fba723d428c27409b61c4176d to your computer and use it in GitHub Desktop.
A Generic validator for getById type of methods from Spring Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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