springMVC集成swagger

来源:互联网 发布:东方财富网龙虎榜数据 编辑:程序博客网 时间:2024/04/30 17:03

swagger原理

后台:后端部分与Java集成,后最终会产生一个json文件。

前台:前台部分就是html、css、js文件,js利用后台产生的json文件构造api;


1、maven配置


2、swaggerConfig.java代码 

package cn.bluemobi.app.controller; 
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 com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.paths.SwaggerPathProvider;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
 
@Configuration
@EnableSwagger
@ComponentScan(basePackages = { "com.mangofactory.swagger" })

public class SwaggerConfig {
    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() {
        SwaggerSpringMvcPlugin plugin = new SwaggerSpringMvcPlugin(this.springSwaggerConfig);
        plugin.apiInfo(apiInfo());
        plugin.includePatterns(".*");
        // GtPaths path = new GtPaths();
        // plugin.pathProvider(path);
        return plugin;
    } 

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("项目的api", "具体的api描述", "http://localhost:8080/demo/api",
                "", "My Apps API Licence Type", "http://localhost:8080/demo");
        return apiInfo;
    } 

    class GtPaths extends SwaggerPathProvider {

        @Override
        public String getOperationPath(String operationPath) {
            String path = super.getOperationPath(operationPath);
            return path + ".htm";
        }


        @Override
        protected String applicationPath() {
            return "/";
        }


        @Override
        protected String getDocumentationPath() {
            return "/";
        }
    }
}

三、项目结构


以上配置参考:

swagger整合spring mvc的doc 点击打开链接

swagger注解说明     点击打开链接


原创粉丝点击