只需要三个步骤即可完成springboot集成swagger2,界面使用bootstrap-ui。
1.在pom.xml文件里添加依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2,创建swagger配置类:
类上加上三个注解
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; 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 @EnableSwagger2 @EnableSwaggerBootstrapUI public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //控制器位置 .apis(RequestHandlerSelectors.basePackage("com.example.swaggerbootui.controller")) .paths(PathSelectors.any()) //表示扫描所有路径 .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("swagger-bootstrap-ui RESTful APIs") //文档的标题,可自定义 .description("swagger-bootstrap-ui") //测试的接口文档,可自定义 .termsOfServiceUrl("http://localhost:8080/") // .contact("1111@qq.com") //联系人信息,可自定义 .version("1.0") //版本信息,可自定义 .build(); } }
3.在控制器类上加入注解@Api(tags = "接口测试")
同时在每个接口方法上加入注解@ApiOperation("名称自定义")
@Api(tags = "用户接口测试") //名称可自定义
@RequestMapping("user")
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("add")
@ApiOperation("添加用户") //名称可自定义
public String add(User user){
userService.save(user);
return "添加成功";
}
@GetMapping("findAll")
@ApiOperation("查找所有用户") //名称可自定义
public List<User> findAll(){
return userService.findAll();
}
@PostMapping("delete")
@ApiOperation("根据id删除用户") //名称可自定义
public String delete(Long id){
userService.deleteById(id);
return "删除成功";
}
}
启动项目后,浏览器输入
http://localhost:8080/doc.html
即可进入swagger-bootstrap-ui界面
至此springboot集成swagger-bootstrap-ui完毕。
感谢观看欢迎评论私聊交流技术疑点难点。