UnitedApiPathProps
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "api.path")
public class UnitedApiPathProps {
String globalPrefix = "api";
}
WebMvcConfig
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Resource
private UnitedApiPathProps pathProps;
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix(
pathProps.getGlobalPrefix(),
c -> c.isAnnotationPresent(UnitedController.class));
}
}
application.yml
api:
path:
global-prefix: api
Controller
@CrossOrigin
@UnitedController("/admin")
public class AdminController {
// ......
}
只要使用了 @UnitedController
注解的 Controller 请求前缀都是 /api
。