博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot异常处理errorController
阅读量:5104 次
发布时间:2019-06-13

本文共 5321 字,大约阅读时间需要 17 分钟。

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

你可能感兴趣的文章
mmap和MappedByteBuffer
查看>>
Linux的基本操作
查看>>
转-求解最大连续子数组的算法
查看>>
对数器的使用
查看>>
OracleOraDb11g_home1TNSListener服务启动后停止,某些服务在未由其他服务或程序使用时将自己主动停止...
查看>>
Redis用户添加、分页、登录、注册、加关注案例
查看>>
练习2
查看>>
【ASP.NET】演绎GridView基本操作事件
查看>>
ubuntu无法解析主机错误与解决的方法
查看>>
尚学堂Java面试题整理
查看>>
MySQL表的四种分区类型
查看>>
[BZOJ 3489] A simple rmq problem 【可持久化树套树】
查看>>
STM32单片机使用注意事项
查看>>
swing入门教程
查看>>
好莱坞十大导演排名及其代表作,你看过多少?
查看>>
Loj #139
查看>>
StringBuffer是字符串缓冲区
查看>>
hihocoder1187 Divisors
查看>>
java入门
查看>>
Spring 整合 Redis
查看>>