Skip to content

eamiear/swagger2-springmvc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

springfox-swagger2

swaggerUI + springmvc 2.0版本

demo

http://172.16.8.60:8080/swagger2

demo中集成了springfox.petstore的示例代码,可以参考使用或者直接查看API文档。

maven方式添加

    <dependency>
    	<groupId>io.springfox</groupId>
    	<artifactId>springfox-swagger2</artifactId>
    	<version>2.6.1</version>
    </dependency>

依赖关系

dependency

具体在maven搜索页面浏览: mavensearch.io 如果不使用maven等配置文件的方式添加依赖包,则需要手动将依赖的jar添加到开发环境中

构造配置类

    
    package com.gosuncn.web.swagger.SwaggerConfig
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    
    @Configuration
    @EnableWebMvc
    @EnableSwagger2
    //@ComponentScan(basePackages = {"com.gosuncn.web"})
    public class SwaggerConfig {
    
    	@Bean
    	public Docket swaggerSpringMvcPlugin(){
    		Docket docket = new Docket(DocumentationType.SWAGGER_2);
    		ApiInfo apiInfo = new ApiInfo("Restfull API","", null, null, null, null, null);
    		docket.apiInfo(apiInfo);
    		return docket;
    	}
    
    }

Reference:

Docket Spring Java Configuration

transitioning-to-v2.md

上下文容器配置

applicationContext.xml文件中添加以下配置项

    <bean name="applicationSwaggerConfig" class="com.gosuncn.web.swagger.SwaggerConfig" />

如果需要开启 CORS支持,在Tomcatweb.xml文件中添加以下配置

   <filter>
	  <filter-name>CorsFilter</filter-name>
	  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
	</filter>
	<filter-mapping>
	  <filter-name>CorsFilter</filter-name>
	  <url-pattern>/*</url-pattern>
	</filter-mapping>

运行

  • 启动项目,浏览器中输入项目链接,如:localhost:8080/swagger2-springmvc/
  • swagger-springmvc已将SwaggerUI集成到项目中,输入上述链接时,服务将定位到localhost:8080/swagger2-springmvc/docs/index.html
  • 可以定位到webapp/docs目录下浏览SwaggerUI文件。
  • 如果SwaggerUI集成到服务中,应该取消CORS支持,否则无法通过localhost的方式访问接口服务。
  • SwaggerUI独立于项目的情况,即SwaggerUI和接口服务分别部署在不同服务器下时,接口服务需要启动CORS支持。打开http://172.16.8.60:8080/apis/,并填入Restfull API服务链接即可。

相关文档/API

About

springmvc 集成 Restfull API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages