Springboot异常处理errorController

package com.hoyan.config;

import com.hoyan.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.WebRequest;

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

/*import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;*/

/**
* web错误 全局配置
* .
*/
@Controller
@Slf4j
public class AppErrorController implements ErrorController {
private static final String ERROR_PATH = "/error";

private ErrorAttributes errorAttributes;

@Override
public String getErrorPath() {
return ERROR_PATH;
}

@Autowired
public AppErrorController(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}

/**
* Web页面错误处理
*/
@RequestMapping(value = ERROR_PATH, produces = "text/html")
public String errorPageHandler(HttpServletRequest request, HttpServletResponse response) {
int status = response.getStatus();
switch (status) {
case 403:
return "403";
case 404:
return "404";
case 500:
return "500";
}

return "index";
}

/**
* 除Web页面外的错误处理,比如Json/XML等
*/
@RequestMapping(value = ERROR_PATH)
@ResponseBody
@ExceptionHandler(value = {Exception.class})
public ApiResponse errorApiHandler(HttpServletRequest request, final Exception ex, final WebRequest req) {

RequestAttributes requestAttributes = new ServletRequestAttributes(request);
log.info(ex.getMessage()+"------------------"+ex.getStackTrace());
Map<String, Object> attr = this.errorAttributes.getErrorAttributes(req, false);
int status = getStatus(request);

return ApiResponse.ofMessage(status, String.valueOf(attr.getOrDefault("message", "error")));
}

private int getStatus(HttpServletRequest request) {
Integer status = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (status != null) {
return status;
}

return 500;
}
}
--------------------------------------------------------------------
package com.hoyan.utils;

/**
* API格式封装
* Created by ljh
*/
public class ApiResponse {
private int code;
private String message;
private Object data;
private boolean more;

public ApiResponse(int code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}

public ApiResponse() {
this.code = Status.SUCCESS.getCode();
this.message = Status.SUCCESS.getStandardMessage();
}

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 Object getData() {
return data;
}

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

public boolean isMore() {
return more;
}

public void setMore(boolean more) {
this.more = more;
}

public static ApiResponse ofMessage(int code, String message) {
return new ApiResponse(code, message, null);
}

public static ApiResponse ofSuccess(Object data) {
return new ApiResponse(Status.SUCCESS.getCode(), Status.SUCCESS.getStandardMessage(), data);
}

public static ApiResponse ofStatus(Status status) {
return new ApiResponse(status.getCode(), status.getStandardMessage(), null);
}

public enum Status {
SUCCESS(200, "OK"),
BAD_REQUEST(400, "Bad Request"),
NOT_FOUND(404, "Not Found"),
INTERNAL_SERVER_ERROR(500, "Unknown Internal Error"),
NOT_VALID_PARAM(40005, "Not valid Params"),
NOT_SUPPORTED_OPERATION(40006, "Operation not supported"),
NOT_LOGIN(50000, "Not Login");

private int code;
private String standardMessage;

Status(int code, String standardMessage) {
this.code = code;
this.standardMessage = standardMessage;
}

public int getCode() {
return code;
}

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

public String getStandardMessage() {
return standardMessage;
}

public void setStandardMessage(String standardMessage) {
this.standardMessage = standardMessage;
}
}
}

转载于:https://www.cnblogs.com/jiahaoJAVA/p/10087267.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值