Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
java-tutorials/api-error-handling/hello-controller-with-exception-handler
@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
You can’t perform that action at this time.