欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

SpringMvc之集成Swagger

发布时间:2025/5/22 javascript 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。