首先在原有的springmvc工程的pom文件中增加swagger
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
增加swagger的配置類
package com.founder.fwpt.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; 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 public class SpringfoxConfig { @Bean public Docket petApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.founder.fwpt.controller")).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("服務平台 API").description("").termsOfServiceUrl("http://localhost:8080").version("1.0").build(); } }
其中有個信息
basePackage
指定controller的包。
在spring-mvc的攔截其中增加靜態資源訪問控制。
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
啟動項目,在瀏覽器中訪問
http://localhost:8080/swagger-ui.html
即可。
具體的controller中的@Api注解信息,參考