轉:https://www.jianshu.com/p/ce7e247515f5?utm_source=oschina-app

:本文是基於springboot配置實現,但在實際中使用springmvc和本文的配置基本一致,不影響使用。

下面是第一種配置方式。儘量精簡的配置。

1 在pom檔案中引入依賴

<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>

2 新建swag配置檔案
在程式碼中新定義一個類,程式碼如下

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.apiInfo(apiInfo());
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("對外開放介面API 文件")
.description("HTTP對外開放介面")
.version("1.0.0")
.termsOfServiceUrl("http://xxx.xxx.com")
.license("LICENSE")
.licenseUrl("http://xxx.xxx.com")
.build();
}
}

3 在自己的controller中使用

@Slf4j
@RestController
public class MyController {
@RequestMapping(value = "hello", method = RequestMethod.GET)
public String hello() {
return "hello";
}
}

如上的配置就可以了,下面是展示效果
在本地啟動專案,然後瀏覽器輸入:

http://localhost:8080/swagger-ui.html
效果如下:

 
swagger.png

這種配置方式非常簡單,對原有的程式碼也基本沒有任何侵入性。基本可以滿足介面描述的需求。

但是,有很多專案都是前後端分離的,在nginx中會配置把 "/rest/" 開頭的連結打到後端來,而把其他請求打到前端去。當然,你可以修改nginx的配置,把某些連結打到前端去,剩下的直接打到後端。不過這種方式會有一定的安全性問題,可控性也不太好。最好能給修改swagger的展示地址,
比如從
http://localhost:8080/swagger-ui.html
修改為
http://localhost:8080/rest/api/doc/swagger-ui.html

下面就是第二種配置方式,可以自定義展示連結。

1 在pom檔案中引入依賴(注意我們去掉了對springfox-swagger-ui的依賴

<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>

2 git clone swagger-ui專案

https://github.com/swagger-api/swagger-ui
請選擇2.0以上,3.0以下版本

將其中的dist資料夾拷貝到自己專案中的resources/swagger目錄下,如圖

 
dist.png

3 在resources下新建swagger.properties檔案,其中的內容為

springfox.documentation.swagger.v2.path=/rest/api/doc

4 修改resources/swagger/dist 下的index檔案
將其中的

  <script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://petstore.swagger.io/v2/swagger.json";
}

url = "http://petstore.swagger.io/v2/swagger.json"
修改為
url = "/rest/api/doc"
修改後如下

  <script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "/rest/api/doc";
}

5 新建swag配置檔案
在程式碼中新定義一個類,程式碼如下

@Configuration
@EnableSwagger2
@PropertySource("classpath:swagger.properties") // 新增對swagger.properties 的引入
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build()
.apiInfo(apiInfo());
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("對外開放介面API 文件")
.description("HTTP對外開放介面")
.version("1.0.0")
.termsOfServiceUrl("http://xxx.xxx.com")
.license("LICENSE")
.licenseUrl("http://xxx.xxx.com")
.build();
}
}

6 增加url對映
如果是springboot,在Application中增加

@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/rest/api/doc/**").addResourceLocations("classpath:/swagger/dist/");
}
.....
}

如果是springmvc,則在api-servlet.xml(application.xml)中增加

<mvc:resources mapping="/rest/api/doc/**" location="classpath:/swagger/dist/"/>

終於配置完成,啟動專案,在瀏覽器中輸入:

http://localhost:8080/rest/api/doc/index.html

 
swagger.png

springfox官網

http://springfox.github.io/springfox/

api註解相關描述可參考

http://blog.csdn.net/xupeng874395012/article/details/68946676