diff --git a/spring-exception-controlleradvice/.classpath b/spring-exception-controlleradvice/.classpath new file mode 100644 index 0000000..d67f3e2 --- /dev/null +++ b/spring-exception-controlleradvice/.classpath @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-exception-controlleradvice/pom.xml b/spring-exception-controlleradvice/pom.xml new file mode 100644 index 0000000..3086b49 --- /dev/null +++ b/spring-exception-controlleradvice/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + com.hmkcode + spring-exception-controlleradvice + 1.0-SNAPSHOT + jar + + spring-boot + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + com.hmkcode.App + + + + + diff --git a/spring-exception-controlleradvice/src/main/java/com/hmkcode/App.java b/spring-exception-controlleradvice/src/main/java/com/hmkcode/App.java new file mode 100644 index 0000000..c977ea6 --- /dev/null +++ b/spring-exception-controlleradvice/src/main/java/com/hmkcode/App.java @@ -0,0 +1,15 @@ +package com.hmkcode; + +import org.springframework.boot.*; +import org.springframework.boot.autoconfigure.*; +import org.springframework.stereotype.*; + +@Controller +@SpringBootApplication +public class App { + + + public static void main(String[] args) throws Exception { + SpringApplication.run(App.class, args); + } +} \ No newline at end of file diff --git a/spring-exception-controlleradvice/src/main/java/com/hmkcode/controllers/Controller.java b/spring-exception-controlleradvice/src/main/java/com/hmkcode/controllers/Controller.java new file mode 100644 index 0000000..f51e398 --- /dev/null +++ b/spring-exception-controlleradvice/src/main/java/com/hmkcode/controllers/Controller.java @@ -0,0 +1,21 @@ +package com.hmkcode.controllers; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +public class Controller { + + + @RequestMapping("/ok") + public @ResponseBody String ok() throws Exception { + return "OK"; + } + + @RequestMapping("/exception") + public @ResponseBody String exception() throws Exception { + throw new Exception("Error"); + } +} diff --git a/spring-exception-controlleradvice/src/main/java/com/hmkcode/exception/ControllerAdvisor.java b/spring-exception-controlleradvice/src/main/java/com/hmkcode/exception/ControllerAdvisor.java new file mode 100644 index 0000000..027ab1f --- /dev/null +++ b/spring-exception-controlleradvice/src/main/java/com/hmkcode/exception/ControllerAdvisor.java @@ -0,0 +1,26 @@ +package com.hmkcode.exception; + + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.http.HttpStatus; + +@ControllerAdvice +public class ControllerAdvisor { + + + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + + public @ResponseBody String generalException(final Exception exception, + final HttpServletRequest request) { + + return exception.getMessage()+" while calling: "+request.getRequestURI(); + + } +} +