SpringBoot:接口请求添加统一的前缀
一、使用spring-boot-starter-web内嵌Tomcat在application.properties中,添加如下配置:# SpringBoot 2.*版本server.servlet.context-path: /api二、使用外置Tomcat + 修改server.xml修改Tomcat中的server.xml配置,添加如下配置:<Service name="Catalin
·
一、使用spring-boot-starter-web内嵌Tomcat
在application.properties中,添加如下配置:
# SpringBoot 2.*版本
server.servlet.context-path: /api
二、使用外置Tomcat + 修改server.xml
修改Tomcat中的server.xml配置,添加如下配置:
<Service name="Catalina">
<Connector port="8001" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" maxParameterCount="1000" maxSpareThreads="750" maxThreads="1000" minSpareTHreads="50" acceptCount="1000" useBodyEncodingForURI="true"/>
<Engine name="Catalina" defaultHost="localhost" jvmRoute="s1">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps" unpackWARs="false" autoDeploy="false" xmlValidation="false" xmlNamespaceAware="false">
<Context path="/api" docBase="目录" reloadable="true"/>
</Host>
</Engine>
</Service>
三、使用外置Tomcat + 自定义WebMvcConfigurer
自定义WebMvcConfigurer
@Configuration
public class ApiPrefixConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer
.addPathPrefix("/api", c -> c.isAnnotationPresent(Controller.class))
.addPathPrefix("/api", c -> c.isAnnotationPresent(RestController.class));
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)