Skip to content

Commit c13c514

Browse files
committed
spring-exception-controlleradvice
spring-exception-controlleradvice
1 parent ce2f73f commit c13c514

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5+
<classpathentry kind="output" path="target/classes"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
7+
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot-starter-web/2.4.2/spring-boot-starter-web-2.4.2.jar"/>
8+
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot-starter/2.4.2/spring-boot-starter-2.4.2.jar"/>
9+
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot/2.4.2/spring-boot-2.4.2.jar"/>
10+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/5.3.3/spring-core-5.3.3.jar"/>
11+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-jcl/5.3.3/spring-jcl-5.3.3.jar"/>
12+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/5.3.3/spring-context-5.3.3.jar"/>
13+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/5.3.3/spring-aop-5.3.3.jar"/>
14+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/5.3.3/spring-beans-5.3.3.jar"/>
15+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/5.3.3/spring-expression-5.3.3.jar"/>
16+
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot-autoconfigure/2.4.2/spring-boot-autoconfigure-2.4.2.jar"/>
17+
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot-starter-logging/2.4.2/spring-boot-starter-logging-2.4.2.jar"/>
18+
<classpathentry kind="var" path="M2_REPO/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar"/>
19+
<classpathentry kind="var" path="M2_REPO/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar"/>
20+
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar"/>
21+
<classpathentry kind="var" path="M2_REPO/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar"/>
22+
<classpathentry kind="var" path="M2_REPO/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar"/>
23+
<classpathentry kind="var" path="M2_REPO/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar"/>
24+
<classpathentry kind="var" path="M2_REPO/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar"/>
25+
<classpathentry kind="var" path="M2_REPO/org/yaml/snakeyaml/1.27/snakeyaml-1.27.jar"/>
26+
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot-starter-json/2.4.2/spring-boot-starter-json-2.4.2.jar"/>
27+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-web/5.3.3/spring-web-5.3.3.jar"/>
28+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar"/>
29+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar"/>
30+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar"/>
31+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.11.4/jackson-datatype-jdk8-2.11.4.jar"/>
32+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.4/jackson-datatype-jsr310-2.11.4.jar"/>
33+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/module/jackson-module-parameter-names/2.11.4/jackson-module-parameter-names-2.11.4.jar"/>
34+
<classpathentry kind="var" path="M2_REPO/org/springframework/boot/spring-boot-starter-tomcat/2.4.2/spring-boot-starter-tomcat-2.4.2.jar"/>
35+
<classpathentry kind="var" path="M2_REPO/org/apache/tomcat/embed/tomcat-embed-core/9.0.41/tomcat-embed-core-9.0.41.jar"/>
36+
<classpathentry kind="var" path="M2_REPO/org/glassfish/jakarta.el/3.0.3/jakarta.el-3.0.3.jar"/>
37+
<classpathentry kind="var" path="M2_REPO/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.41/tomcat-embed-websocket-9.0.41.jar"/>
38+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-webmvc/5.3.3/spring-webmvc-5.3.3.jar"/>
39+
</classpath>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.hmkcode</groupId>
6+
<artifactId>spring-exception-controlleradvice</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-boot</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
</properties>
18+
<parent>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-parent</artifactId>
21+
<version>2.4.2</version>
22+
</parent>
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
30+
</dependencies>
31+
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.codehaus.mojo</groupId>
37+
<artifactId>exec-maven-plugin</artifactId>
38+
<version>3.0.0</version>
39+
<configuration>
40+
<mainClass>com.hmkcode.App</mainClass>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.hmkcode;
2+
3+
import org.springframework.boot.*;
4+
import org.springframework.boot.autoconfigure.*;
5+
import org.springframework.stereotype.*;
6+
7+
@Controller
8+
@SpringBootApplication
9+
public class App {
10+
11+
12+
public static void main(String[] args) throws Exception {
13+
SpringApplication.run(App.class, args);
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.hmkcode.controllers;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.ResponseBody;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
8+
@RestController
9+
public class Controller {
10+
11+
12+
@RequestMapping("/ok")
13+
public @ResponseBody String ok() throws Exception {
14+
return "OK";
15+
}
16+
17+
@RequestMapping("/exception")
18+
public @ResponseBody String exception() throws Exception {
19+
throw new Exception("Error");
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.hmkcode.exception;
2+
3+
4+
import javax.servlet.http.HttpServletRequest;
5+
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
import org.springframework.web.bind.annotation.ResponseStatus;
10+
import org.springframework.http.HttpStatus;
11+
12+
@ControllerAdvice
13+
public class ControllerAdvisor {
14+
15+
16+
@ExceptionHandler(Exception.class)
17+
@ResponseStatus(HttpStatus.BAD_REQUEST)
18+
19+
public @ResponseBody String generalException(final Exception exception,
20+
final HttpServletRequest request) {
21+
22+
return exception.getMessage()+" while calling: "+request.getRequestURI();
23+
24+
}
25+
}
26+

0 commit comments

Comments
 (0)