springboot項目在所有的controller里加上統一前綴


import com.oigit.imsapi.common.WebInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    /**
     * 請求路徑添加統一前綴
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
       //注解添加 
    //configurer.addPathPrefix(
"/imsApi", c -> c.isAnnotationPresent(Controller.class) || c.isAnnotationPresent(RestController.class));
    //包添加
    configurer.addPathPrefix("/robotApi", c -> c.getPackage().getName().contains("com.xxx.xxx.action"));
}
 @Bean public WebInterceptor getInstance(){ return new WebInterceptor(); } /** * 攔截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getInstance()) .addPathPatterns("/**") .excludePathPatterns("/imsApi/login/**");
 } /** * 跨域支持 * @param registry */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowCredentials(true) .allowedHeaders("*") .allowedOrigins("*") .allowedMethods("*") .maxAge(3600); } }

 


免責聲明!

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



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