转自:
http://blog.csdn.net/opopopwqwqwq/article/details/75087748最近学习使用jeesite开源框架,发现没有集成swagger,所以就尝试集成一下,权做学习记录
1.下载swagger UI
https://github.com/swagger-api/swagger-ui/releases
我按引用博客中下载的2.x的版本,新版本我看结构与旧版已有所不同加入了springboot一些东西,springboot还没看暂时搁置使用旧版
下载下来的UI如果放到WEB-INF下 spring-mvc 配置需要添加相应的静态资源映射
我直接放到外面了 jeesite的static下
2.jar引用
jeesite框架pom只需引入三个即可
- <!-- swagger -->
- <dependency>
- <groupId>com.mangofactory</groupId>
- <artifactId>swagger-springmvc</artifactId>
- <version>1.0.2</version>
- </dependency>
- <dependency>
- <groupId>com.mangofactory</groupId>
- <artifactId>swagger-models</artifactId>
- <version>1.0.2</version>
- </dependency>
- <dependency>
- <groupId>com.wordnik</groupId>
- <artifactId>swagger-annotations</artifactId>
- <version>1.3.11</version>
- </dependency>
3.配置
- package com.thinkgem.jeesite.common.web;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- 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;
- /**
- * @author xiegx
- * @version 创建时间:2016-8-16 下午2:01:10
- * SwaggerUI配置
- */
- @Configuration
- @EnableSwagger
- @EnableWebMvc
- @ComponentScan(basePackages ={"com.thinkgem.jeesite.modules.sys.web"})
- public class SwaggerConfig extends WebMvcConfigurerAdapter {
- private SpringSwaggerConfig springSwaggerConfig;
- /**
- * Required to autowire SpringSwaggerConfig
- */
- @Autowired
- public 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.
- */
- @Bean
- public SwaggerSpringMvcPlugin customImplementation()
- {
- return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
- .apiInfo(apiInfo())
- .includePatterns(".*")
- .swaggerGroup("XmPlatform")
- .apiVersion("1.0.0");
- }
- @Override
- public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
- configurer.enable();
- }
- /*
- * "标题 title",
- * "描述 description",
- * "termsOfServiceUrl",
- * "联系邮箱 contact email",
- * "许可证的类型 license type",
- * "许可证的链接 license url"
- */
- private ApiInfo apiInfo()
- {
- ApiInfo apiInfo = new ApiInfo(
- "Jeesite平台API文档",
- "后台RESTful API",
- "",//
- "admin@xmplatform.com",
- "",
- "");
- return apiInfo;
- }
- }
spring-mvc.xml
- <context:component-scan base-package="com.thinkgem.jeesite.modules.sys.web" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
- </context:component-scan>
4.修改index.html
jeesite\src\main\webapp\static\swagger\dist\index.html
- <!-- Some basic translations -->
- <!-- <script src='lang/translator.js' type='text/javascript'></script> -->
- <!-- <script src='lang/ru.js' type='text/javascript'></script> -->
- <!-- <script src='lang/en.js' type='text/javascript'></script> -->
- <!-- 中文翻译 -->
- <script src='lang/translator.js' type='text/javascript'></script>
- <script src='lang/zh-cn.js' type='text/javascript'></script>
- <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
- url = "http://127.0.0.1:8080/jeesite/api-docs";
- }
- // Pre load translate...
http://ip:port/项目名/api-docs
5.启动项目访问http://127.0.0.1:8080/jeesite/static/swagger/dist/index.html
效果初步显示,即使没添加swagger那些注解,也生成了很多文档