SpringBoot接口服务处理Whitelabel Error Page
转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50915979
- 《SpringBoot接口服务处理Whitelabel Error Page》
- 《Maven依赖载入错误的情况分析》
- 《Java Webproject转换为基于Maven的Webproject》
- 《Maven Webproject执行异常:Maven.multiModuleProjectDirectory system propery is not set的问题》
- 《Maven执行异常:Exception in thread “main” java.lang.UnsupportedClassVersionError》
1.0 异常说明
SpringBoot搭建的接口服务。假设请求非注冊类的无效接口地址,则返回该页面。主要问题就是没有对异常请求做处理。
举例,定义有效接口地址如:http://ip/user, http://ip/age。则其他地址均为无效地址,若请求则返回上述Whitelabel Error Page页面。
2.0 异常处理
主要是加入一个AppErrorController的Controller类,这里我定义了异常返回页面。
package com.autonavi.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
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.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
/**
* <p>Author: loongshawn
* <p>Date: 16-03-17
* <p>Version: 1.0
*/
@Controller
public class AppErrorController implements ErrorController{
private static final Logger logger = LoggerFactory.getLogger(AppErrorController.class);
private static AppErrorController appErrorController;
/**
* Error Attributes in the Application
*/
@Autowired
private ErrorAttributes errorAttributes;
private final static String ERROR_PATH = "/error";
/**
* Controller for the Error Controller
* @param errorAttributes
* @return
*/
public AppErrorController(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}
public AppErrorController() {
if(appErrorController == null){
appErrorController = new AppErrorController(errorAttributes);
}
}
/**
* Supports the HTML Error View
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH, produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request) {
return new ModelAndView("greeting", getErrorAttributes(request, false));
}
/**
* Supports other formats like JSON, XML
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH)
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* Returns the path of the error page.
*
* @return the error path
*/
@Override
public String getErrorPath() {
return ERROR_PATH;
}
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
Map<String, Object> map = this.errorAttributes.getErrorAttributes(requestAttributes,includeStackTrace);
String URL = request.getRequestURL().toString();
map.put("URL", URL);
logger.debug("AppErrorController.method [error info]: status-" + map.get("status") +", request url-" + URL);
return map;
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode != null) {
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
}
}
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
这个类实现了ErrorController接口,用来处理请求的各种异常。当中定义了一个greeting的html模版,用来显示返回结果。初始化此类模版须要在pom中加入下面依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
这个依赖主要是给SpringBoot中载入html等类型的模版服务。其支持的模版类型例如以下:
Template modes:
[THYMELEAF] * XHTML
[THYMELEAF] * XML
[THYMELEAF] * HTML5
[THYMELEAF] * LEGACYHTML5
[THYMELEAF] * VALIDXHTML
[THYMELEAF] * VALIDXML
SpringBoot项目配置模版路径的方法例如以下:
1、在main的resources路径下新建templates目录
2、在templates目录中新建模版文件greeting.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Error Pages</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Url:' + ${URL}" />
<p th:text="'Error:' + ${error}" />
<p th:text="'Status:' + ${status}" />
<p th:text="'Timestamp:' + ${timestamp}" />
</body>
</html>
3.0 处理结果
无效请求地址均会返回此页面,仅仅是当中的返回值不同。
SpringBoot接口服务处理Whitelabel Error Page的更多相关文章
- SpringBoot接口服务处理Whitelabel Error Page(转)
To switch it off you can set server.error.whitelabel.enabled=false http://stackoverflow.com/question ...
- 《Springboot极简教程》问题解决:Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping for(转)
13.2 Spring Boot启动报错:Whitelabel Error Page 13.2 Spring Boot启动报错:Whitelabel Error Page 问题描述 Whitelabe ...
- SpringBoot入门报错 Whitelabel Error Page的总结
刚入门SpringBoot,编写helloControl类,去访问本地端口,无缘无故报了这个错误 Whitelabel Error Page 总结了下,目前我碰到的有三种会导致这种情况 1.当你的 S ...
- 关于Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as ...
- SpringBoot Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
使用SpringBoot写HelloWorld,当配置好启动类后,再创建新的controller或其它类,启动项目后访问对应的映射名,页面显示: Whitelabel Error Page This ...
- 新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo
新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo ...
- springboot报错Whitelabel Error Page
第一次使用springboot没有问题.隔了两天继续看.一直报错Whitelabel Error Page. 重新搭建试了任何方法都错了. 报的就是一个404错误,犯了一个习惯性错误,一般都是loca ...
- SpringBoot启动项目之后,访问页面出现Whitelabel Error Page
话说万事具备,只欠东风- 蹭闲暇时来跑个SpringBoot项目玩玩,把一切配置依赖准备就绪之后打算运行项目. Staring...... 接着,在浏览器输入地址 localhost:8080/hel ...
- springboot项目出现Whitelabel Error Page的问题
springboot项目出现Whitelabel Error Page的问题 大概就是这种情况,然而昨天还是没问题的,通过对比就发现,是自己手欠了 简单来说解决办法就是将springboot的启动项目 ...
随机推荐
- 解决win8/8.1系统安装.net framework 3.5出现0x800F0906代码错误
解决方案一. 首先打开windows更新,检查是否有系统更新要安装,因为这个问题可能是导致.net 3.5无法安装的罪魁祸首,要检查windows更新,可以右键“这台电脑”点击“属性”,打开后,点击左 ...
- 作用JavaScript访问和操作数据库
JS操作 Access 数据库 <SCRIPT LANGUAGE="JavaScript"> <!-- var filePath = location.href. ...
- swfupload js中 file 对象的属性
name=3cc68cfc60b87e6dd6887d8a.jpg modificationdate=Wed Apr 21 15:48:30 UTC+0800 2010 filestatus=-1 ...
- 语音信号处理之(一)动态时间规整(DTW)
语音信号处理之(一)动态时间规整(DTW) zouxy09@qq.com 原文:http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门课,快考试了,所以也要 ...
- Netbeans配合xdebug调试
http://xdebug.org/download.php 下载对应的xdebug的dll不知道php文件中的ext文件夹中 并且加入以下配置在php.info 然后重启apche zend_ext ...
- sar详解(linux/unix通用)
转载:http://www.toxingwang.com/linux-unix/2027.html sar是linux和unix通用的性能报告工具,常用命令组合如下: sar 命令行的常用格式: sa ...
- Java内存溢出的详细解决方案(转http://developer.51cto.com/art/200906/129346.htm)
一.内存溢出类型 1.java.lang.OutOfMemoryError: PermGen space JVM管理两种类型的内存,堆和非堆.堆是给开发人员用的上面说的就是,是在JVM启动时创建:非堆 ...
- Dreamweaver如何设置自动换行,修改字体
1 打开和关闭自动换行功能 查看-代码视图选项 2 调整字体大小和类别
- org.codehaus.xfire.fault.XFireFault: Could not read XML stream.. Nested exception is javax.xml.strea
xfire使用中出现故障: 1. [2014-04-16 14:51:07.564]-[ERROR] org.apache.struts2.dispatcher.Dispatcher Exceptio ...
- PHP Filesysten函数
PHP 5 Filesystem 函数 PHP Filesystem 简介 Filesystem 函数允许您访问和操作文件系统. 安装 Filesystem 函数是 PHP 核心的组成部分.无需安装即 ...