From 2041089e74f2070b47ae927feec9a109d7fa2a71 Mon Sep 17 00:00:00 2001 From: Jacob Stopak Date: Sun, 21 Apr 2019 13:13:28 -0700 Subject: [PATCH 1/4] Add code for BAEL-2892 v0.0.1: pass-exception-client-json-spring-boot --- .../.gitignore | 29 +++++++++++++ .../pom.xml | 43 +++++++++++++++++++ .../CustomException.java | 11 +++++ .../ErrorHandler.java | 21 +++++++++ .../MainController.java | 20 +++++++++ ...tionToClientJsonSpringBootApplication.java | 13 ++++++ .../src/main/resources/application.properties | 1 + .../src/main/resources/static/.gitignore | 4 ++ .../src/main/resources/templates/.gitignore | 4 ++ ...oClientJsonSpringBootApplicationTests.java | 16 +++++++ 10 files changed, 162 insertions(+) create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/.gitignore create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/CustomException.java create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplication.java create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/application.properties create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/static/.gitignore create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/templates/.gitignore create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplicationTests.java diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/.gitignore b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/.gitignore new file mode 100644 index 0000000000..153c9335eb --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/.gitignore @@ -0,0 +1,29 @@ +HELP.md +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml new file mode 100644 index 0000000000..28a699ad98 --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.4.RELEASE + + + com.baeldung + pass-exception-to-client-json-spring-boot + 0.0.1-SNAPSHOT + pass-exception-to-client-json-spring-boot + Baeldung article code on how to pass exceptions to client in JSON format using Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/CustomException.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/CustomException.java new file mode 100644 index 0000000000..56fe80cd9a --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/CustomException.java @@ -0,0 +1,11 @@ +package com.baeldung.passexceptiontoclientjsonspringboot; + +public class CustomException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public CustomException() { + super("Custom exception message."); + } + +} \ No newline at end of file diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java new file mode 100644 index 0000000000..823689adb9 --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java @@ -0,0 +1,21 @@ +package com.baeldung.passexceptiontoclientjsonspringboot; + +import org.springframework.http.HttpStatus; +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; + +@ControllerAdvice +public class ErrorHandler { + + @ExceptionHandler(CustomException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ResponseBody + public CustomException handleCustomException(CustomException ce) { + + return ce; + + } + +} \ No newline at end of file diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java new file mode 100644 index 0000000000..68df38fb73 --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java @@ -0,0 +1,20 @@ +package com.baeldung.passexceptiontoclientjsonspringboot; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class MainController { + + @GetMapping("/") + public String index() throws CustomException { + + if (true) { + throw new CustomException(); + } + + return "index"; + + } + +} \ No newline at end of file diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplication.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplication.java new file mode 100644 index 0000000000..77833c28fd --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.passexceptiontoclientjsonspringboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PassExceptionToClientJsonSpringBootApplication { + + public static void main(String[] args) { + SpringApplication.run(PassExceptionToClientJsonSpringBootApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/application.properties b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/static/.gitignore b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/static/.gitignore new file mode 100644 index 0000000000..5e7d2734cf --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/static/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/templates/.gitignore b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/templates/.gitignore new file mode 100644 index 0000000000..5e7d2734cf --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/resources/templates/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplicationTests.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplicationTests.java new file mode 100644 index 0000000000..5e35c21f4c --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/PassExceptionToClientJsonSpringBootApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung.passexceptiontoclientjsonspringboot; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PassExceptionToClientJsonSpringBootApplicationTests { + + @Test + public void contextLoads() { + } + +} From b5baab70929f8fa7cd83ca51f807d0ebdc3e0a74 Mon Sep 17 00:00:00 2001 From: Jacob Stopak Date: Sun, 21 Apr 2019 15:22:14 -0700 Subject: [PATCH 2/4] Move @ResponseBody to class level, bump version to 0.0.2 --- .../pass-exception-to-client-json-spring-boot/pom.xml | 2 +- .../passexceptiontoclientjsonspringboot/ErrorHandler.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml index 28a699ad98..d592cae351 100644 --- a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml @@ -10,7 +10,7 @@ com.baeldung pass-exception-to-client-json-spring-boot - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT pass-exception-to-client-json-spring-boot Baeldung article code on how to pass exceptions to client in JSON format using Spring Boot diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java index 823689adb9..d69f60b75e 100644 --- a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java @@ -7,11 +7,11 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; @ControllerAdvice +@ResponseBody public class ErrorHandler { @ExceptionHandler(CustomException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - @ResponseBody public CustomException handleCustomException(CustomException ce) { return ce; From 8463567986d58dac5cfbb60971a7ba95840354f3 Mon Sep 17 00:00:00 2001 From: Jacob Stopak Date: Tue, 23 Apr 2019 15:57:57 -0700 Subject: [PATCH 3/4] Minor refactor, bump to version 0.0.3 --- .../pass-exception-to-client-json-spring-boot/pom.xml | 2 +- .../MainController.java | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml index d592cae351..33a3aba03b 100644 --- a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml @@ -10,7 +10,7 @@ com.baeldung pass-exception-to-client-json-spring-boot - 0.0.2-SNAPSHOT + 0.0.3-SNAPSHOT pass-exception-to-client-json-spring-boot Baeldung article code on how to pass exceptions to client in JSON format using Spring Boot diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java index 68df38fb73..82c0c022a8 100644 --- a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/MainController.java @@ -7,14 +7,8 @@ import org.springframework.web.bind.annotation.GetMapping; public class MainController { @GetMapping("/") - public String index() throws CustomException { - - if (true) { - throw new CustomException(); - } - - return "index"; - + public void index() throws CustomException { + throw new CustomException(); } } \ No newline at end of file From a2002dde521fe0191a5d73c438aa75ee99e58300 Mon Sep 17 00:00:00 2001 From: Jacob Stopak Date: Tue, 23 Apr 2019 23:38:51 -0700 Subject: [PATCH 4/4] Add integration test, bump version to 0.0.4 --- .../pom.xml | 2 +- .../ErrorHandler.java | 2 -- .../MainControllerIntegrationTest.java | 16 ++++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/MainControllerIntegrationTest.java diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml index 33a3aba03b..8237955aef 100644 --- a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/pom.xml @@ -10,7 +10,7 @@ com.baeldung pass-exception-to-client-json-spring-boot - 0.0.3-SNAPSHOT + 0.0.4-SNAPSHOT pass-exception-to-client-json-spring-boot Baeldung article code on how to pass exceptions to client in JSON format using Spring Boot diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java index d69f60b75e..b39d2811dd 100644 --- a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/main/java/com/baeldung/passexceptiontoclientjsonspringboot/ErrorHandler.java @@ -13,9 +13,7 @@ public class ErrorHandler { @ExceptionHandler(CustomException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public CustomException handleCustomException(CustomException ce) { - return ce; - } } \ No newline at end of file diff --git a/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/MainControllerIntegrationTest.java b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/MainControllerIntegrationTest.java new file mode 100644 index 0000000000..a615dc5096 --- /dev/null +++ b/spring-boot-exceptions/pass-exception-to-client-json-spring-boot/src/test/java/com/baeldung/passexceptiontoclientjsonspringboot/MainControllerIntegrationTest.java @@ -0,0 +1,16 @@ +package com.baeldung.passexceptiontoclientjsonspringboot; + +import org.junit.Test; + +public class MainControllerIntegrationTest { + + @Test(expected = CustomException.class) + public void givenIndex_thenCustomException() throws CustomException { + + MainController mainController = new MainController(); + + mainController.index(); + + } + +} \ No newline at end of file