jeesite框架下学习尝试集成swagger

转自:

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只需引入三个即可

[html]  view plain  copy
  1. <!-- swagger -->  
  2. <dependency>  
  3.     <groupId>com.mangofactory</groupId>  
  4.     <artifactId>swagger-springmvc</artifactId>  
  5.     <version>1.0.2</version>  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>com.mangofactory</groupId>  
  9.     <artifactId>swagger-models</artifactId>  
  10.     <version>1.0.2</version>  
  11. </dependency>  
  12. <dependency>  
  13.     <groupId>com.wordnik</groupId>  
  14.     <artifactId>swagger-annotations</artifactId>  
  15.     <version>1.3.11</version>  
  16. </dependency>   


3.配置

[java]  view plain  copy
  1. package com.thinkgem.jeesite.common.web;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.ComponentScan;  
  5. import org.springframework.context.annotation.Configuration;  
  6. import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;  
  7. import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
  9. import com.mangofactory.swagger.configuration.SpringSwaggerConfig;  
  10. import com.mangofactory.swagger.models.dto.ApiInfo;  
  11. import com.mangofactory.swagger.plugin.EnableSwagger;  
  12. import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;  
  13.   
  14.   
  15. /** 
  16.  * @author xiegx 
  17.  * @version 创建时间:2016-8-16 下午2:01:10 
  18.  * SwaggerUI配置 
  19.  */  
  20. @Configuration  
  21. @EnableSwagger  
  22. @EnableWebMvc  
  23. @ComponentScan(basePackages ={"com.thinkgem.jeesite.modules.sys.web"})    
  24. public class SwaggerConfig extends WebMvcConfigurerAdapter {    
  25.   
  26.     private SpringSwaggerConfig springSwaggerConfig;  
  27.   
  28.     /** 
  29.      * Required to autowire SpringSwaggerConfig 
  30.      */  
  31.     @Autowired  
  32.     public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)  
  33.     {  
  34.         this.springSwaggerConfig = springSwaggerConfig;  
  35.     }  
  36.   
  37.     /** 
  38.      * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc 
  39.      * framework - allowing for multiple swagger groups i.e. same code base 
  40.      * multiple swagger resource listings. 
  41.      */  
  42.     @Bean  
  43.     public SwaggerSpringMvcPlugin customImplementation()  
  44.     {  
  45.         return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)  
  46.                 .apiInfo(apiInfo())  
  47.                 .includePatterns(".*")  
  48.                 .swaggerGroup("XmPlatform")  
  49.                 .apiVersion("1.0.0");  
  50.     }  
  51.   
  52.     @Override    
  53.     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {    
  54.       configurer.enable();    
  55.     }    
  56.     /* 
  57.      * "标题 title", 
  58.      * "描述 description",  
  59.      * "termsOfServiceUrl",  
  60.      * "联系邮箱 contact email", 
  61.      * "许可证的类型 license type",  
  62.      * "许可证的链接 license url" 
  63.      */  
  64.     private ApiInfo apiInfo()  
  65.     {  
  66.         ApiInfo apiInfo = new ApiInfo(  
  67.                 "Jeesite平台API文档",  
  68.                 "后台RESTful API",  
  69.                 "",//  
  70.                 "admin@xmplatform.com",  
  71.                 "",  
  72.                 "");  
  73.         return apiInfo;  
  74.     }  
  75. }  
@ComponentScan(basePackages ={"com.thinkgem.jeesite.modules.sys.web"})既扫描的位置


spring-mvc.xml

[html]  view plain  copy
  1. <context:component-scan base-package="com.thinkgem.jeesite.modules.sys.web" use-default-filters="false">  
  2.     <context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>  
  3. </context:component-scan>  


4.修改index.html

jeesite\src\main\webapp\static\swagger\dist\index.html

[javascript]  view plain  copy
  1. <!-- Some basic translations -->  
  2. <!-- <script src='lang/translator.js' type='text/javascript'></script> -->  
  3. <!-- <script src='lang/ru.js' type='text/javascript'></script> -->  
  4. <!-- <script src='lang/en.js' type='text/javascript'></script> -->  
  5. <!-- 中文翻译 -->  
  6. <script src='lang/translator.js' type='text/javascript'></script>  
  7. <script src='lang/zh-cn.js' type='text/javascript'></script>  
  8.   
  9. <script type="text/javascript">  
  10.   $(function () {  
  11.     var url = window.location.search.match(/url=([^&]+)/);  
  12.     if (url && url.length > 1) {  
  13.       url = decodeURIComponent(url[1]);  
  14.     } else {  
  15.       //url = "http://petstore.swagger.io/v2/swagger.json";  
  16.       //替换为本项目的url  
  17.       url = "http://127.0.0.1:8080/jeesite/api-docs";  
  18.     }  
  19.   
  20.     // Pre load translate...  

http://ip:port/项目名/api-docs


5.启动项目访问http://127.0.0.1:8080/jeesite/static/swagger/dist/index.html



效果初步显示,即使没添加swagger那些注解,也生成了很多文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值