All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wudaosoft.spring.boot.autoconfigure.web.RestErrorController Maven / Gradle / Ivy

The newest version!
/**
 *    Copyright 2009-2018 Wudao Software Studio(wudaosoft.com)
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.wudaosoft.spring.boot.autoconfigure.web;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author changsoul.wu
 *
 */
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class RestErrorController extends BasicErrorController {

	/**
	 * @param errorAttributes
	 * @param errorProperties
	 */
	public RestErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
		super(errorAttributes, errorProperties);
	}

	/**
	 * @param errorAttributes
	 * @param errorProperties
	 * @param errorViewResolvers
	 */
	public RestErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties,
			List errorViewResolvers) {
		super(errorAttributes, errorProperties, errorViewResolvers);
	}

	@Override
	@RequestMapping(produces = "text/html")
	public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
		HttpStatus status = getStatus(request);
		response.setStatus(status.value());
		response.setContentType(MediaType.TEXT_HTML_VALUE);
		response.setCharacterEncoding("utf-8");

		String result = null;

		if (HttpStatus.NOT_FOUND.equals(status)) {
			// response.setContentType(MediaType.TEXT_HTML_VALUE);

			Map model = Collections.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL)));
			result = "\n\n404 Not Found\n\n

Not Found

\n" + "

The requested URL " + model.get("path") + " was not found on this server.

\n"; // result = "

Not Found Page

" + "

This application has no explicit mapping for " // + model.get("path") + ", so you are seeing this as a fallback.

" + "
" // + model.get("timestamp") + "
" + "
There was an unexpected error (type=" // + model.get("error") + ", status=" + model.get("status") + ").
" + "
" // + model.get("message") + "
"; } else { // response.setContentType(MediaType.APPLICATION_JSON_VALUE); // result = "{\"errCode\": " + model.get("status") + ", \"errMsg\": \"" + model.get("error") + "\"}"; // // result = "

Information Page

" + "

There are something bad to happen. For more information please set the \"Accept\" with \"application/json;charset=UTF-8\" or \"application/xml;charset=UTF-8\" for HTTP request header.

"; result = "\n\nWarning\n\n

Warning

\n" + "

There are something bad happening for this requested.

\n"; } try { response.getWriter().append(result); } catch (IOException e) { } return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy