springboot集成swagger(swagger可以自动生成API接口文档),gradle作为项目管理工具:
1、gradle下载swagger依赖的jar包,在 https://mvnrepository.com/ 分别搜索 springfox-swagger2 和 springfox-swagger-ui,选择一个下载量最多的版本,两个jar包版本一定要一致!!!
// https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'
// https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
2、在springboot里配置swagger。本地目录如下,需要修改的类为:EsbApplication启动类;config中的SwaggerConfig配置类和controller里的EsbCotroller
3、config下新建SwaggerConfig类,配置swagger
package **.**.**.config; //自己的package路径
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer{
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("io.transwarp.esb"))
.paths(PathSelectors.any())
.build();
}
//可以看到 swagger-ui.html 在META-INF/resources目录下,需要手动的将静态资源路径指向这里
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
//指定静态资源指向的另一种写法。这时,需要类extends WebMvcConfigurationSupport,然后Override以下
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("/**")
// .addResourceLocations("classpath:/static/");
// registry.addResourceHandler("swagger-ui.html")
// .addResourceLocations("classpath:/META-INF/resources/");
// registry.addResourceHandler("/webjars/**")
// .addResourceLocations("classpath:/META-INF/resources/webjars/");
// super.addResourceHandlers(registry);
// }
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
.description("API 描述")
.termsOfServiceUrl("http://www.baidu.com/")
.version("1.0")
.build();
}
}
4、controller里新建EsbCotroller类,使用swagger注解。其中,@Api是类注解 @ApiOperation是方法注解,其具体写法参考 swagger注解
package **.**.**.controller; //个人项目package路径
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(description = "userRequestPath")
public class EsbController {
@ApiOperation(value = "esb",notes = "esb")
@RequestMapping(value = "/esb",method= RequestMethod.GET)
public String esb(){
return "123";
}
}
5、EsbApplication启动类添加swagger注解
@SpringBootApplication(scanBasePackages = "**") //package路径
@EnableSwagger2
public class EsbApplication {
public static void main(String[] args) {
SpringApplication.run(EsbApplication.class, args);
}
}
6、配置完成后,启动项目,在浏览器输入http://localhost:8080/swagger-ui.html .
至此,成功完成配置!!!