转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50915979

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的更多相关文章

  1. SpringBoot接口服务处理Whitelabel Error Page(转)

    To switch it off you can set server.error.whitelabel.enabled=false http://stackoverflow.com/question ...

  2. 《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 ...

  3. SpringBoot入门报错 Whitelabel Error Page的总结

    刚入门SpringBoot,编写helloControl类,去访问本地端口,无缘无故报了这个错误 Whitelabel Error Page 总结了下,目前我碰到的有三种会导致这种情况 1.当你的 S ...

  4. 关于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 ...

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

  6. 新建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 ...

  7. springboot报错Whitelabel Error Page

    第一次使用springboot没有问题.隔了两天继续看.一直报错Whitelabel Error Page. 重新搭建试了任何方法都错了. 报的就是一个404错误,犯了一个习惯性错误,一般都是loca ...

  8. SpringBoot启动项目之后,访问页面出现Whitelabel Error Page

    话说万事具备,只欠东风- 蹭闲暇时来跑个SpringBoot项目玩玩,把一切配置依赖准备就绪之后打算运行项目. Staring...... 接着,在浏览器输入地址 localhost:8080/hel ...

  9. springboot项目出现Whitelabel Error Page的问题

    springboot项目出现Whitelabel Error Page的问题 大概就是这种情况,然而昨天还是没问题的,通过对比就发现,是自己手欠了 简单来说解决办法就是将springboot的启动项目 ...

随机推荐

  1. chromium的安装程序

    转自:http://www.xue163.com/182/6/1822338.html#pinglun 这篇文章主要简单讲解chromium的安装程序mini_installer 在编译mini_in ...

  2. Page的ResolveClientUrl与ResolveUrl读取路径

    Page的ResolveClientUrl与ResolveUrl读取路径 . 一.Page对象的ResolveClientUrl与ResolveUrl Page.ResolveClientUrl(): ...

  3. JQuery 动态提交form

    function exportExcel() { var merchantName = $('#merchantName').val(); var merchantNo = $('#merchantN ...

  4. iOS: sqlite数据库的基本操作

    介绍: sqlite3(3是版本)是本地系统中的一个小型数据库,因为它没有在数据维护和安全上做过多的操作,所以它存储处理数据时,非常简单方便,但是它是不安全和不可靠的,如果一旦误操作删除了数据,是没有 ...

  5. windows安装dcm4chee 出错 check file system group LOSSY_STORAGE for deletion

    错误情景: 解决方法: 更改服务的监听端口(参考DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1)

  6. TestNG的组測试和组中组測试

    在编写測试的过程中,我们常常遇到仅仅想运行个别或者某一部分/某一类型的測试用例,这时我们能够使用TestNG的分组測试方法 分组測试在配置时.TestNG运行的原则是:仅仅保留最小集合进行运行 看代码 ...

  7. SVM训练结果参数说明 训练参数说明 归一化加快速度和提升准确率 归一化还原

    原文:http://blog.sina.com.cn/s/blog_57a1cae80101bit5.html 举例说明 svmtrain -s 0 -?c 1000 -t 1 -g 1 -r 1 - ...

  8. [Grunt] Watch && grunt-contrib-watch

    Watch is an essential component of any Grunt build, and you will find yourself using it in almost ev ...

  9. (剑指Offer)面试题58:二叉树的下一个结点

    题目: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 思路: 考虑中序遍历的过程, 如果当前结点存在右子节点, ...

  10. KineticJS教程(7)

    KineticJS教程(7) 作者: ysm 7.图形变换 7.1.线性变化 Kinetic提供了一个图形对象的transitionTo(config)方法实现图形的线性变换,也就是从原始的状态线性变 ...