jeesite框架下学习尝试集成swagger(1)

引用:

http://blog.youkuaiyun.com/pingyan158/article/details/53514987

http://blog.youkuaiyun.com/wangjun5159/article/details/47283125



最近学习使用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;
    }
}
@ComponentScan(basePackages ={"com.thinkgem.jeesite.modules.sys.web"})既扫描的位置


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那些注解,也生成了很多文档

具体细化调整还需继续学习...


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值