转发地址请求至swagger地址

起因不能直接链接工作预发环境的IP地址,只能通过域名访问。但因多个微服务组成的系统存在,需保持每个服务的健康检查API需要一致性,故通过修改 server.servlet.context-path  的方式不能使用。

查询很多资料,最后想到通过 spring的拦截器进行请求转发

直接贴转发器代码

package com.wwdz.content.web.config;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * SLOGAN: 做一个会写四种茴,笨且自律的人
 *
 * @Author Yasser
 * @Date 2019-12-20
 * @Description
 */

@Configuration
public class UrlFilter implements Filter {

    private static String urlPath = "/api/content/web";

    private List swaggerList = Arrays.asList(
            urlPath + "/swagger",
            urlPath + "/webjars",
            urlPath + "/v2/api-docs");

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;

        String path = httpRequest.getRequestURI();

        for (String key : swaggerList) {
            if (path.indexOf(key) >= 0) {
                System.out.print(httpRequest.getRequestURI());
                path = path.replaceAll(urlPath, "");
                System.out.println(" :----: " + path);
                httpRequest.getRequestDispatcher(path).forward(request, response);
                return;
            }
        }
        chain.doFilter(request, response);
    }
}

 

你可能感兴趣的:(异常解决)