SpringBoot的error用全局异常去处理

记录一下使用SpringBoot2.0.5的error用全局异常去处理

在使用springboot时,当访问的http地址或者说是请求地址输错后,会返回一个页面,如下:
SpringBoot的error用全局异常去处理_第1张图片
这是因为请求的地址不存在,默认会显示error页面 但我们实际需要一个接口,看到的效果是这样的
SpringBoot的error用全局异常去处理_第2张图片
pom.xml

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<groupId>boot.example.error</groupId>
	<artifactId>boot-example-error-2.0.5</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-example-error-2.0.5</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<!-- 单元测试组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<!-- web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- 热部署devtools工具组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!-- 修改后自动编译启动 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<!--fork : 如果没有该项配置,devtools不会起作用,即应用不会restart -->
					<fork>true</fork>
				</configuration>
			</plugin>
			<!-- 这个插件可以将应用打包成一个可执行的jar包 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

App.java

package boot.example.error;

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


@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
        System.out.println( "Hello World!" );
    }
}

BaseErrorController.java

package boot.example.error.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import boot.example.error.exception.BaseErrorException;


/**
 * 
 * error用全局异常去处理
 *
 */
@Controller
@RequestMapping(value = "error")
public class BaseErrorController implements ErrorController{

	@RequestMapping
	public String error() {
		return getErrorPath();
	}

	@Override
	public String getErrorPath() {
		throw new BaseErrorException("访问错误");
	}

}

IndexController.java

package boot.example.error.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/")
public class IndexController {
	
	@RequestMapping("index")
	@ResponseBody
	public String index() {		
		return "hello world";
	}
	
	
}

BaseErrorException.java

package boot.example.error.exception;

public class BaseErrorException extends RuntimeException {

	private String msg;
    
    public BaseErrorException(String msg) {
    	this.msg = msg;
    }

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}


}

GlobalDefaultExceptionHandler.java

package boot.example.error.globalexception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import boot.example.error.exception.BaseErrorException;
import boot.example.error.response.Response;

@ControllerAdvice
public class GlobalDefaultExceptionHandler {
	
    @ExceptionHandler(BaseErrorException.class)
    @ResponseBody
    public Response defaultExceptionHandler(BaseErrorException e) {
        return new Response(false,100,e.getMsg());
    }
	
}

Response.java

package boot.example.error.response;

public class Response {
	
    private boolean state;

    private int code;

    private String msg;

    private Object data;

    private long timestamp;

    public Response() {}

    public Response(boolean state, int code, String msg) {
        this.state = state;
        this.code = code;
        this.msg = msg;
        this.timestamp = System.currentTimeMillis()/1000;
    }

    public Response(boolean state, int code, String msg, Object data) {
        this.state = state;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.timestamp = System.currentTimeMillis()/1000;
    }

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }


    @Override
    public String toString() {
        return "InsResponse{" +
                "state=" + state +
                ", code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                '}';
    }
}

启动项目使用postman来测试

http://localhost:8080/index/myyhtw
http://localhost:8080/index

没有加error全局异常处理的情况
SpringBoot的error用全局异常去处理_第3张图片
加了error全局异常处理的情况
SpringBoot的error用全局异常去处理_第4张图片
可以看到Status都是404的

你可能感兴趣的:(SpringBoot+Demo,springboot,error异常处理,error页面,错误页面处理)