欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法

 更新時(shí)間:2022年02月18日 09:45:32   作者:zhangSir134  
這篇文章主要介紹了springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

controller 增加指定前綴

1、增加配置

server.servlet.context-path: /api

這種是最常見的,加上這個(gè)配置后,所有的url,必須帶上/api的前綴,才能訪問到該url

2、過濾攔截

這種是加上/api也可以訪問,不加/api也可以訪問,適合項(xiàng)目重構(gòu)修改的適合用

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Configuration
@Order(1)
@WebFilter(filterName = "urlFilter", urlPatterns = "/api/*")
public class UrlFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String servletPath = httpRequest.getServletPath();
        if (StringUtils.isNotBlank(servletPath) && servletPath.startsWith("/api")) {
            String newPath = servletPath.substring(4);
            request.getRequestDispatcher(newPath).forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }
    @Override
    public void destroy() {
    }
}

springboot服務(wù)端口、項(xiàng)目前綴的配置

在application.properties中配置

server.port: 8081
server.context-path: /demo

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論