亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

此應用程序沒有針對/ 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 關注
  • 5813 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號