SpringBoot复习:(37)自定义ErrorController

所有接口统一返回的数据格式

package cn.edu.tju.domain;

public class MyResponse {
    private int code;
    private String message;
    private String exception;
    private String stack;

    public int getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public String getStack() {
        return stack;
    }

    public void setStack(String stack) {
        this.stack = stack;
    }
}

自定义的ErrorController覆盖默认的BasicErrorController。当出错时,也统一返回自定义的数据格式,而不是Spring的ResponseEntity

package cn.edu.tju.controller;

import cn.edu.tju.domain.MyResponse;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/error")
public class MyErrorController extends AbstractErrorController {


    public MyErrorController(ErrorAttributes errorAttributes, List errorViewResolvers) {
        super(errorAttributes, errorViewResolvers);
    }

    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaa");
        HttpStatus status = getStatus(request);
        ErrorAttributeOptions of = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.EXCEPTION,
                ErrorAttributeOptions.Include.MESSAGE,
                ErrorAttributeOptions.Include.EXCEPTION
                //,ErrorAttributeOptions.Include.STACK_TRACE
        );
        Map model = Collections
                .unmodifiableMap(getErrorAttributes(request, of));
        response.setStatus(status.value());
        ModelAndView modelAndView = resolveErrorView(request, response, status, model);
        return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
    }

    @RequestMapping
    @ResponseBody
    public MyResponse error(HttpServletRequest request) {
        HttpStatus status = getStatus(request);
        if (status == HttpStatus.NO_CONTENT) {
            MyResponse myResponse = new MyResponse();
            myResponse.setCode(204);
            return myResponse;
        }
        ErrorAttributeOptions of = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.EXCEPTION,
                ErrorAttributeOptions.Include.MESSAGE,
                ErrorAttributeOptions.Include.EXCEPTION,
                ErrorAttributeOptions.Include.STACK_TRACE);
        Map body = getErrorAttributes(request, of);
        MyResponse myResponse = new MyResponse();
        myResponse.setCode(200);
        myResponse.setMessage(String.valueOf(body.get("message")));
        myResponse.setException(String.valueOf(body.get("exception")));
        myResponse.setStack(String.valueOf(body.get("trace")));
        //path
        //timestamp
        //error

        return myResponse;
    }
}

SpringBoot默认异常处理自动配置类ErrorMvcAutoConfiguration

你可能感兴趣的:(SpringBoot,spring,boot,后端,java)