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;
}
}
}