From dbff9a3e13053d9b78535c542311b640563b4ac3 Mon Sep 17 00:00:00 2001 From: wangkang Date: Tue, 8 Aug 2017 11:44:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9404=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/handle/ExceptionController.java | 118 ++++++++++++++++++ .../handle/ExceptionControllerConfig.java | 57 +++++++++ 2 files changed, 175 insertions(+) create mode 100644 udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionController.java create mode 100644 udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionControllerConfig.java diff --git a/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionController.java b/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionController.java new file mode 100644 index 0000000..7a1c487 --- /dev/null +++ b/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionController.java @@ -0,0 +1,118 @@ +package org.itkk.udf.core.exception.handle; + +import org.itkk.udf.core.RestResponse; +import org.itkk.udf.core.exception.ErrorResult; +import org.springframework.boot.autoconfigure.web.AbstractErrorController; +import org.springframework.boot.autoconfigure.web.ErrorAttributes; +import org.springframework.boot.autoconfigure.web.ErrorProperties; +import org.springframework.boot.autoconfigure.web.ErrorViewResolver; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.NoHandlerFoundException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.*; + +/** + * ErrorController + */ +@Controller +@RequestMapping("${server.error.path:${error.path:/error}}") +public class ExceptionController extends AbstractErrorController { + + /** + * errorProperties + */ + private final ErrorProperties errorProperties; + + /** + * Create a new {@link ExceptionController} instance. + * + * @param errorAttributes the error attributes + * @param errorProperties configuration properties + */ + public ExceptionController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) { + this(errorAttributes, errorProperties, new ArrayList<>()); + } + + /** + * Create a new {@link ExceptionController} instance. + * + * @param errorAttributes the error attributes + * @param errorProperties configuration properties + * @param errorViewResolvers error view resolvers + */ + public ExceptionController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers) { + super(errorAttributes, errorViewResolvers); + Assert.notNull(errorProperties, "ErrorProperties must not be null"); + this.errorProperties = errorProperties; + } + + @Override + public String getErrorPath() { + return this.errorProperties.getPath(); + } + + /** + * html错误 + * + * @param request request + * @param response response + * @return ModelAndView + */ + @RequestMapping(produces = "text/html") + public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { + HttpStatus status = getStatus(request); + Map model = Collections.unmodifiableMap(getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML))); + response.setStatus(status.value()); + ModelAndView modelAndView = resolveErrorView(request, response, status, model); + return (modelAndView == null ? new ModelAndView("error", model) : modelAndView); + } + + /** + * json错误 + * + * @param request request + * @return ResponseEntity + */ + @RequestMapping + @ResponseBody + public ResponseEntity> error(HttpServletRequest request) { + Map body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL)); + HttpStatus status = getStatus(request); + ErrorResult errorResult = new ErrorResult(); + errorResult.setType(NoHandlerFoundException.class.getName()); + errorResult.setMessage("No handler found for " + request.getMethod() + " " + body.get("path").toString()); + errorResult.setDate(new Date()); + return new ResponseEntity<>(new RestResponse<>(status, errorResult), status); + } + + /** + * Determine if the stacktrace attribute should be included. + * + * @param request the source request + * @param produces the media type produced (or {@code MediaType.ALL}) + * @return if the stacktrace attribute should be included + */ + protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) { + ErrorProperties.IncludeStacktrace include = getErrorProperties().getIncludeStacktrace(); + return include == ErrorProperties.IncludeStacktrace.ALWAYS || include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM && getTraceParameter(request); + } + + /** + * Provide access to the error properties. + * + * @return the error properties + */ + protected ErrorProperties getErrorProperties() { + return this.errorProperties; + } + +} diff --git a/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionControllerConfig.java b/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionControllerConfig.java new file mode 100644 index 0000000..d2f23da --- /dev/null +++ b/udf-starter-core/src/main/java/org/itkk/udf/core/exception/handle/ExceptionControllerConfig.java @@ -0,0 +1,57 @@ +package org.itkk.udf.core.exception.handle; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.web.*; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.DispatcherServlet; + +import javax.servlet.Servlet; +import java.util.List; + +/** + * ErrorControllerConfig + */ +@Configuration +@ConditionalOnWebApplication +@ConditionalOnClass({Servlet.class, DispatcherServlet.class}) +@AutoConfigureBefore(WebMvcAutoConfiguration.class) +@EnableConfigurationProperties(ResourceProperties.class) +public class ExceptionControllerConfig { + /** + * errorViewResolvers + */ + @Autowired(required = false) + private List errorViewResolvers; + + /** + * serverProperties + */ + private final ServerProperties serverProperties; + + /** + * 构造函数 + * + * @param serverProperties serverProperties + */ + public ExceptionControllerConfig(ServerProperties serverProperties) { + this.serverProperties = serverProperties; + } + + /** + * exceptionController + * + * @param errorAttributes errorAttributes + * @return ExceptionController + */ + @Bean + public ExceptionController exceptionController(ErrorAttributes errorAttributes) { + return new ExceptionController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers); + } + + +}