javascript
SpringMvc之集成Swagger
1 搭建springmvc環境(此處省略)
?
2 導入額外需要的相關jar
swagger-springmvc-1.0.0.jar
swagger-models-1.0.0.jar
swagger-core-1.5.0.jar
swagger-annotations-1.3.11.jar
jackson-core-2.4.4.jar
jackson-annotations-2.4.0.jar
guava-15.0.jar
classmate-1.1.0.jar
?
3 新建MySwaggerConfig配置文件
package com.zns;import com.mangofactory.swagger.configuration.SpringSwaggerConfig; import com.mangofactory.swagger.models.dto.ApiInfo; import com.mangofactory.swagger.plugin.EnableSwagger; import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration @EnableSwagger @EnableWebMvc public class MySwaggerConfig {private SpringSwaggerConfig springSwaggerConfig;/*** Required to autowire SpringSwaggerConfig*/@Autowiredpublic void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig){this.springSwaggerConfig = springSwaggerConfig;}/*** Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc* framework - allowing for multiple swagger groups i.e. same code base* multiple swagger resource listings.*/@Beanpublic SwaggerSpringMvcPlugin customImplementation(){return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?");}private ApiInfo apiInfo(){ApiInfo apiInfo = new ApiInfo("My Apps API Title","My Apps API Description","My Apps API terms of service","My Apps API Contact Email","My Apps API Licence Type","My Apps API License URL");return apiInfo;} }?
?
4 在springmvc配置文件加入
<bean class="com.zns.MySwaggerConfig" />?
?
5 從https://github.com/swagger-api/swagger-ui 下載swagger-ui
?
6 在WebContent中新建一個swagger文件夾(可以任意取名),然后將下載的swagger-ui解壓后將dist下的所有文件放到swagger文件夾下
?
7 修改swagger/index.html文件,默認是從連接http://petstore.swagger.io/v2/swagger.json獲取 API 的 JSON,這里需要將url值修改為http://{ip}:{port}/{projectName}/api-docs的形式
? 比如http://localhost:8080/項目名/api-docs
?
8 啟動項目
訪問http://localhost:8080/項目名/swagger/index.html,即可看到接口列表
?
經測試 springmvc4.0.0 + swagger-ui-2.1.2 可用!
?
轉載于:https://www.cnblogs.com/zengnansheng/p/10385833.html
總結
以上是生活随笔為你收集整理的SpringMvc之集成Swagger的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员日常工作中如何正确的偷懒?
- 下一篇: 12JDBC