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() { + } + +}