org.zodiac.autoconfigure.web.ServletHttpContextAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.web;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootConfiguration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(value = {javax.servlet.Servlet.class, org.springframework.web.servlet.DispatcherServlet.class})
public class ServletHttpContextAutoConfiguration implements WebMvcConfigurer {
private final HttpContextProperties httpContextProperties;
public ServletHttpContextAutoConfiguration(HttpContextProperties httpContextProperties) {
this.httpContextProperties = httpContextProperties;
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
/*请求路径添加统一前缀。*/
String basePath = httpContextProperties.getBasePath();
configurer.addPathPrefix(basePath, c -> (c.isAnnotationPresent(Controller.class) || c.isAnnotationPresent(RestController.class)));
}
}