为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁花如伊 2019-12-25 14:30:01
我用maven编写了教程https://spring.io/guides/gs/uploading-files/复制了我使用的所有代码。该应用程序可以运行,但是出现错误:Whitelabel Error Page此应用程序没有针对/ error的显式映射,因此您将其视为后备。Tue Jun 30 17:24:02 CST 2015有一个意外错误(类型=未找到,状态= 404)。无讯息我该如何解决?
查看完整描述

3 回答

?
饮歌长啸

TA贡献1951条经验 获得超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;


/**

 * Basic Controller which is called for unhandled errors

 */

@Controller

public class AppErrorController implements ErrorController{


    /**

     * Error Attributes in the Application

     */

    private ErrorAttributes errorAttributes;


    private final static String ERROR_PATH = "/error";


    /**

     * Controller for the Error Controller

     * @param errorAttributes

     */

    public AppErrorController(ErrorAttributes errorAttributes) {

        this.errorAttributes = errorAttributes;

    }


    /**

     * Supports the HTML Error View

     * @param request

     * @return

     */

    @RequestMapping(value = ERROR_PATH, produces = "text/html")

    public ModelAndView errorHtml(HttpServletRequest request) {

        return new ModelAndView("/errors/error", getErrorAttributes(request, false));

    }


    /**

     * Supports other formats like JSON, XML

     * @param request

     * @return

     */

    @RequestMapping(value = ERROR_PATH)

    @ResponseBody

    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {

        Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));

        HttpStatus status = getStatus(request);

        return new ResponseEntity<Map<String, Object>>(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<String, Object> getErrorAttributes(HttpServletRequest request,

                                                   boolean includeStackTrace) {

        RequestAttributes requestAttributes = new ServletRequestAttributes(request);

        return this.errorAttributes.getErrorAttributes(requestAttributes,

                includeStackTrace);

    }


    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;

    }

}

上面的类基于Springs BasicErrorController类。


您可以ErrorController在@Configuration文件中实例化以上内容:


 @Autowired

 private ErrorAttributes errorAttributes;


 @Bean

 public AppErrorController appErrorController(){return new AppErrorController(errorAttributes);}

您可以ErrorAttributes通过实现ErrorAttributes选择覆盖默认值。但在大多数情况下,DefaultErrorAttributes应该足够。


查看完整回答
反对 回复 2019-12-25
  • 3 回答
  • 0 关注
  • 5259 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信