|
@@ -0,0 +1,48 @@
|
|
|
+package org.hsweb.web.controller;
|
|
|
+
|
|
|
+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.MediaType;
|
|
|
+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;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Controller
|
|
|
+@RequestMapping("/error")
|
|
|
+public class DefaultErrorController implements ErrorController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ErrorAttributes errorAttributes;
|
|
|
+
|
|
|
+ @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
|
|
|
+ public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ RequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
|
|
+ Map<String, Object> model = errorAttributes.getErrorAttributes(requestAttributes, true);
|
|
|
+ int code = ((int) model.get("code"));
|
|
|
+ response.setStatus(code);
|
|
|
+ return new ModelAndView("error/" + response.getStatus(), model);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping
|
|
|
+ @ResponseBody
|
|
|
+ public Object error(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ RequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
|
|
+ Map<String, Object> model = errorAttributes.getErrorAttributes(requestAttributes, true);
|
|
|
+ int code = ((int) model.get("code"));
|
|
|
+ response.setStatus(code);
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getErrorPath() {
|
|
|
+ return "/error";
|
|
|
+ }
|
|
|
+}
|