引入相关依赖
<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>
开启swagger服务,进行详细的配置
package com.kklt.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import static com.google.common.base.Predicates.or;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;
/**
* Created by kklt on 2017/7/13.
* SwaggerConfig
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket testApi() {
return new Docket(DocumentationType.SPRING_WEB)
.groupName("portalUser")
.genericModelSubstitutes(DeferredResult.class)
.genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(true)
.pathMapping("/")
.select()
.paths(or(regex("/user/.*")))
.build()
.apiInfo(portalUserApiInfo());
}
private ApiInfo portalUserApiInfo() {
return new ApiInfoBuilder()
.title("Tesco")
.description("前台用户接口")
.version("1.0")
.termsOfServiceUrl("NO terms of service")
.contact(new Contact("李守余", "http://www.baidu.com", "183327xxxx0@163.com"))
.license("The Apache License, Version 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.build();
}
}
访问 http://localhost:8080/swagger-ui.html