1. 程式人生 > >Spring boot 異常處理配置

Spring boot 異常處理配置

except plugin Delve compiler cep tid user tps shm

1. 新建Maven項目 exception

2. pom.xml

<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>
    <
groupId>com.java</groupId> <artifactId>exception</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>
2.0.5.RELEASE</version> </parent> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!-- 熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

3. ExceptionStarter.java

package com.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExceptionStarter {

    public static void main(String[] args) {
        SpringApplication.run(ExceptionStarter.class, args);
    }

}

4. User.java

package com.java.vo;

public class User {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }

}

5. TestException.java

package com.java.exception;

public class TestException extends RuntimeException {
    private static final long serialVersionUID = -6025232952122532866L;

    private String username;

    public TestException(String username) {
        super("測試演習,用戶名不存在");
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

}

6. ControllerExceptionHandler.java

package com.java.handler;

import java.util.HashMap;
import java.util.Map;

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;

import com.java.exception.TestException;

@ControllerAdvice
public class ControllerExceptionHandler {

    @ResponseBody
    @ExceptionHandler(TestException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> handlerTestException(TestException e) {
        Map<String, Object> result = new HashMap<>();
        result.put("username", e.getUsername());
        result.put("message", e.getMessage());
        return result;
    }

}

7. TestController.java

package com.java.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.exception.TestException;
import com.java.vo.User;

@RestController
public class TestController {

    @GetMapping("/test")
    public User test(User user) {
        System.out.println(user);
        if ("test".equals(user.getName())) {
            throw new TestException(user.getName());
        }
        return user;
    }

}

8. 運行ExceptionStarter.java,啟動測試

瀏覽器輸入 http://localhost:8080/test?name=Logen&age=16

正常場景返回結果如下:

{"name":"Logen","age":16}

瀏覽器輸入 http://localhost:8080/test?name=test&age=16

異常場景返回結果如下:

{"message":"測試演習,用戶名不存在","username":"test"}

搭建完成,配置生效。

.

Spring boot 異常處理配置