SpringBoot 2.0 设置 api前缀,且访问静态资源时不需要加/api前缀,访问接口时需要/api前缀的方法
干掉配置文件中的 server.servlet.context-path=/api
这个类直接复制到项目里:
走你:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix("/api", c -> c.isAnnotationPresent(RestController.class ) || c.isAnnotationPresent(Controller.class));
}
/**
* SpringBoot 2.x要重写该方法,
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/META-INF/resources/")
.addResourceLocations("classpath:/META-INF/resources/image/")
.addResourceLocations("classpath:/resources/")
.addResourceLocations("classpath:/resources/upload/")
.addResourceLocations("classpath:/upload/");
}
}