Created
September 10, 2018 19:42
-
-
Save geraldoyudo/a4ebe86bff11cb6552735b368bfe9ce3 to your computer and use it in GitHub Desktop.
java-tutorials/api-error-handling/hello-controller-with-exception-handler
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
@RestController | |
public class HelloController { | |
@PostMapping(path = "/sayHello") | |
public HelloResponse sayHello(@RequestBody HelloRequest request){ | |
if(request.getName().contains("-")){ | |
throw new SyntaxException(); | |
} | |
return HelloResponse.builder() | |
.message(String.format("Hello %s!!", request.getName())) | |
.build(); | |
} | |
@ExceptionHandler(SyntaxException.class) | |
public ApiError handleSyntaxException(){ | |
return ApiError.builder() | |
.error("Syntax Error (from controller)") | |
.errorCode(991) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment