Add code for BAEL-2892 v0.0.1: pass-exception-client-json-spring-boot

This commit is contained in:
Jacob Stopak 2019-04-21 13:13:28 -07:00
parent 2cd8a93fe5
commit 2041089e74
10 changed files with 162 additions and 0 deletions

View File

@ -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/

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>pass-exception-to-client-json-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pass-exception-to-client-json-spring-boot</name>
<description>Baeldung article code on how to pass exceptions to client in JSON format using Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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.");
}
}

View File

@ -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;
}
}

View File

@ -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";
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

View File

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

View File

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