1. 程式人生 > >jeesite框架下學習嘗試整合swagger(1)

jeesite框架下學習嘗試整合swagger(1)

引用:

http://blog.csdn.net/pingyan158/article/details/53514987

http://blog.csdn.net/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",
                "",//
                "
[email protected]
", "", ""); 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那些註解,也生成了很多文件

具體細化調整還需繼續學習...