1、添加依賴 html
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
2、添加類web
@EnableWebMvc @EnableSwagger2 @Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.z*.b*.c*.controller")) // 注意修改此處的包名 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("接口列表 v1.1.0") // 任意,請稍微規範點 .description("接口測試") // 任意,請稍微規範點 .termsOfServiceUrl("http://url/swagger-ui.html") // 將「url」換成本身的ip:port .contact("laowu") // 無所謂(這裏是做者的別稱) .version("1.1.0") .build(); } }
3、在spring-mvc.xml中添加spring
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
4、類、方法上添加註解api
@ApiOperation(value = "名稱", httpMethod = "POST") @ApiParam(required = true, name = "test", value = "參數")
5、訪問 localhost:8080/項目名/swagger-ui.htmlspring-mvc
6、添加攔截過濾mvc
<mvc:exclude-mapping path="/swagger*/**"></mvc:exclude-mapping> <mvc:exclude-mapping path="/v2/**"></mvc:exclude-mapping> <mvc:exclude-mapping path="/webjars/**"></mvc:exclude-mapping>
轉自:http://www.jb51.net/article/130208.htmapp