SpringBoot接口服务处理Whitelabel Error Page

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

  • 《SpringBoot接口服务处理Whitelabel Error Page》
  • 《Maven依赖加载错误的情况分析》
  • 《Java Web工程转换为基于Maven的Web工程》
  • 《Maven Web工程运行异常:Maven.multiModuleProjectDirectory system propery is not set的问题》
  • 《Maven运行异常:Exception in thread “main” java.lang.UnsupportedClassVersionError》

1.0 异常说明

SpringBoot搭建的接口服务,如果请求非注册类的无效接口地址,则返回该页面。主要问题就是没有对异常请求做处理。

SpringBoot接口服务处理Whitelabel Error Page_第1张图片

举例,定义有效接口地址如: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;


/**
 * 

Author: loongshawn *

Date: 16-03-17 *

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> error(HttpServletRequest request) { Map body = getErrorAttributes(request, getTraceParameter(request)); HttpStatus status = getStatus(request); return new ResponseEntity>(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 getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) { RequestAttributes requestAttributes = new ServletRequestAttributes(request); Map 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中加入以下依赖:


            org.springframework.boot
            spring-boot-starter-thymeleaf

这个依赖主要是给SpringBoot中加载html等类型的模版服务。其支持的模版类型如下:

 Template modes:
[THYMELEAF]     * XHTML
[THYMELEAF]     * XML
[THYMELEAF]     * HTML5
[THYMELEAF]     * LEGACYHTML5
[THYMELEAF]     * VALIDXHTML
[THYMELEAF]     * VALIDXML

SpringBoot项目配置模版路径的方法如下:

1、在main的resources路径下新建templates文件夹

SpringBoot接口服务处理Whitelabel Error Page_第2张图片

2、在templates文件夹中新建模版文件greeting.html


"http://www.thymeleaf.org">

    Error Pages
    "Content-Type" content="text/html; charset=UTF-8" />


    

"'Url:' + ${URL}" />

"'Error:' + ${error}" />

"'Status:' + ${status}" />

"'Timestamp:' + ${timestamp}" />

3.0 处理结果

无效请求地址均会返回此页面,只是其中的返回值不同。

SpringBoot接口服务处理Whitelabel Error Page_第3张图片

你可能感兴趣的:(JAVA,MAVEN,SpringBoot)