CentOS環境下Swagger應用部署指南
在CentOS上部署Swagger應用前,需安裝以下基礎依賴:
sudo yum install -y java-1.8.0-openjdk-devel
java -version # 驗證安裝(需顯示Java版本信息)
sudo yum install -y maven
mvn -version # 驗證安裝(需顯示Maven版本信息)
curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -
sudo yum install -y nodejs
node -v # 驗證安裝(需顯示Node.js版本信息)
npm -v # 驗證安裝(需顯示npm版本信息)
若需將Swagger集成到Spring Boot應用中,實現API文檔自動化生成,步驟如下:
pom.xml
文件中添加以下依賴:<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
SwaggerConfig.java
配置類,指定掃描的控制器包路徑:import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.yourpackage.controller")) // 替換為你的控制器包路徑
.paths(PathSelectors.any())
.build();
}
}
mvn spring-boot:run
),訪問http://<服務器IP>:<應用端口>/swagger-ui.html
(如http://192.168.1.100:8080/swagger-ui.html
),即可查看Swagger UI界面。若需快速搭建Swagger編輯器或UI服務,可使用Docker:
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
docker pull swaggerapi/swagger-editor
docker run -d -p 8080:8080 --name swagger-editor swaggerapi/swagger-editor
訪問http://<服務器IP>:8080
,即可使用Swagger Editor編寫API文檔。docker pull swaggerapi/swagger-ui
docker run -d -p 8081:8081 --name swagger-ui swaggerapi/swagger-ui
訪問http://<服務器IP>:8081
,默認會加載https://petstore.swagger.io/v2/swagger.json
,可通過修改配置指向自定義API文檔。若需手動部署Swagger UI,步驟如下:
mkdir -p /opt/swagger
cd /opt/swagger
wget https://github.com/swagger-api/swagger-ui/archive/v3.34.0.tar.gz
tar -xf v3.34.0.tar.gz
cd swagger-ui-3.34.0
/usr/share/nginx/html
):sudo cp -r dist/* /usr/share/nginx/html/
index.html
文件,將默認的API文檔URL(https://petstore.swagger.io/v2/swagger.json
)替換為你的自定義URL(如http://<服務器IP>:<應用端口>/v2/api-docs
)。sudo systemctl start nginx
sudo systemctl enable nginx
訪問http://<服務器IP>
,即可查看Swagger UI界面。application.properties
或application.yml
文件調整Swagger UI行為,例如:springfox.documentation.swagger.v2.path=/v2/api-docs # API文檔路徑
springfox.documentation.swagger-ui.base-url=/swagger-ui.html # UI入口路徑
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/swagger-ui.html", "/v2/api-docs").hasIpAddress("192.168.1.0/24") # 僅允許192.168.1.0/24網段訪問
.anyRequest().permitAll()
.and()
.csrf().disable();
}
}
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
JAVA_HOME
環境變量是否正確設置:export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
export PATH=$JAVA_HOME/bin:$PATH
pom.xml
中的依賴版本是否兼容(如Spring Boot與Swagger版本匹配)。通過以上步驟,可在CentOS環境下完成Swagger應用的部署與配置,實現API文檔的自動化管理與可視化測試。