[網關異常處理]請求路徑:/captchaImage,異常信息:404 NOT_FOUND


今天用若依二開,在項目里添加了一個模塊,啟動系統的時候報錯了。

前台提示404 NOT FOUND,同時驗證碼無法顯示。
在這里插入圖片描述
這個問題,我一分鍾之內就知道是哪兒的問題。

原因是,我啟動的ruoyi-ui是Vue版本,而我的后台服務器是Cloud版本。

但是,還是想記錄一下,順便把Spring Cloud GateWay的相關知識,再鞏固一遍。

08:53:21.851 [reactor-http-nio-3] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [網關異常處理]請求路徑:/captchaImage,異常信息:404 NOT_FOUND
08:53:55.264 [reactor-http-nio-4] ERROR c.r.g.h.GatewayExceptionHandler - [handle,52] - [網關異常處理]請求路徑:/captchaImage,異常信息:404 NOT_FOUND

可以看到,是GatewayExceptionHandler 的第52行代碼提示了這個錯誤。

GatewayExceptionHandler.java

package com.ruoyi.gateway.handler;

import org.springframework.cloud.gateway.support.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import com.ruoyi.common.core.utils.ServletUtils;
import reactor.core.publisher.Mono;

/** * 網關統一異常處理 */
@Order(-1)
@Configuration
public class GatewayExceptionHandler implements ErrorWebExceptionHandler{

    private static final Logger log = LoggerFactory.getLogger(GatewayExceptionHandler.class);

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex){
    
        ServerHttpResponse response = exchange.getResponse();

        if (exchange.getResponse().isCommitted()){
            return Mono.error(ex);
        }

        String msg;

        if (ex instanceof NotFoundException){
            msg = "服務未找到";
        }
        else if (ex instanceof ResponseStatusException){
            ResponseStatusException responseStatusException = (ResponseStatusException) ex;
            msg = responseStatusException.getMessage();
        }
        else{
            msg = "內部服務器錯誤";
        }

        log.error("[網關異常處理]請求路徑:{},異常信息:{}", exchange.getRequest().getPath(), ex.getMessage());

        return ServletUtils.webFluxResponseWriter(response, msg);
    }
}

不多說,看會學習筆記去。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM