此应用程序没有针对/ error的显式映射

我用maven编写了教程https://spring.io/guides/gs/uploading-files/

复制了我使用的所有代码。


该应用程序可以运行,但是出现错误:


Whitelabel Error Page此应用程序没有针对/ error的显式映射,因此您将其视为后备。Tue Jun 30 17:24:02 CST 2015有一个意外错误(类型=未找到,状态= 404)。无讯息


我该如何解决?


繁花如伊
浏览 5595回答 3
3回答

饮歌长啸

您可以通过ErrorController在应用程序中添加来解决此问题。您可以让错误控制器返回所需的视图。我的应用程序中的错误控制器如下所示: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;import javax.servlet.http.HttpServletRequest;import java.util.Map;/**&nbsp;* Basic Controller which is called for unhandled errors&nbsp;*/@Controllerpublic class AppErrorController implements ErrorController{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Error Attributes in the Application&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private ErrorAttributes errorAttributes;&nbsp; &nbsp; private final static String ERROR_PATH = "/error";&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Controller for the Error Controller&nbsp; &nbsp; &nbsp;* @param errorAttributes&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public AppErrorController(ErrorAttributes errorAttributes) {&nbsp; &nbsp; &nbsp; &nbsp; this.errorAttributes = errorAttributes;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Supports the HTML Error View&nbsp; &nbsp; &nbsp;* @param request&nbsp; &nbsp; &nbsp;* @return&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @RequestMapping(value = ERROR_PATH, produces = "text/html")&nbsp; &nbsp; public ModelAndView errorHtml(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; return new ModelAndView("/errors/error", getErrorAttributes(request, false));&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Supports other formats like JSON, XML&nbsp; &nbsp; &nbsp;* @param request&nbsp; &nbsp; &nbsp;* @return&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @RequestMapping(value = ERROR_PATH)&nbsp; &nbsp; @ResponseBody&nbsp; &nbsp; public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));&nbsp; &nbsp; &nbsp; &nbsp; HttpStatus status = getStatus(request);&nbsp; &nbsp; &nbsp; &nbsp; return new ResponseEntity<Map<String, Object>>(body, status);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Returns the path of the error page.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return the error path&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @Override&nbsp; &nbsp; public String getErrorPath() {&nbsp; &nbsp; &nbsp; &nbsp; return ERROR_PATH;&nbsp; &nbsp; }&nbsp; &nbsp; private boolean getTraceParameter(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; String parameter = request.getParameter("trace");&nbsp; &nbsp; &nbsp; &nbsp; if (parameter == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return !"false".equals(parameter.toLowerCase());&nbsp; &nbsp; }&nbsp; &nbsp; private Map<String, Object> getErrorAttributes(HttpServletRequest request,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean includeStackTrace) {&nbsp; &nbsp; &nbsp; &nbsp; RequestAttributes requestAttributes = new ServletRequestAttributes(request);&nbsp; &nbsp; &nbsp; &nbsp; return this.errorAttributes.getErrorAttributes(requestAttributes,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; includeStackTrace);&nbsp; &nbsp; }&nbsp; &nbsp; private HttpStatus getStatus(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; Integer statusCode = (Integer) request&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getAttribute("javax.servlet.error.status_code");&nbsp; &nbsp; &nbsp; &nbsp; if (statusCode != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HttpStatus.valueOf(statusCode);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return HttpStatus.INTERNAL_SERVER_ERROR;&nbsp; &nbsp; }}上面的类基于Springs BasicErrorController类。您可以ErrorController在@Configuration文件中实例化以上内容:&nbsp;@Autowired&nbsp;private ErrorAttributes errorAttributes;&nbsp;@Bean&nbsp;public AppErrorController appErrorController(){return new AppErrorController(errorAttributes);}您可以ErrorAttributes通过实现ErrorAttributes选择覆盖默认值。但在大多数情况下,DefaultErrorAttributes应该足够。
打开App,查看更多内容
随时随地看视频慕课网APP