本文共 921 字,大约阅读时间需要 3 分钟。
这里有一点要注意:
spring boot错误的页面都会到error的请求中,所以可以用如下的方式统一管理
package com.xxxxx.apc.controller;import com.apc.apc.utils.ResultUtil;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import java.util.HashMap;import java.util.Map;@RestControllerpublic class ErrorController implements org.springframework.boot.autoconfigure.web.ErrorController { @Override public String getErrorPath() { return "error"; } @RequestMapping("/error") public Object handleError(HttpServletRequest request){ Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); if(statusCode == 404){ Mapmap = new HashMap<>(); map.put("tip", "can not find page"); return ResultUtil.error(map); } return null; }}
运行截图如下:
转载地址:http://vdmif.baihongyu.com/