一、首先实现Springboot整合Swagger2生成
我自己使用的是 springboot 2.1.0,默认8080端口
springboot依赖 :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
swagger依赖:
<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>
Springboot加上swagger注解:
@SpringBootApplication
@EnableSwagger2
public class SpringbootSwagger2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootSwagger2Application.class, args);
}
}
配置swagger:
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restfun风格")
.termsOfServiceUrl("http://blog.youkuaiyun.com/saytime")
.version("1.0")
.build();
}
}
测试的 controller(只加了swagger简单注释):
@RestController
@RequestMapping("/")
public class UserController extends BaseController{
@Autowired
private UserManager userManager;
@ApiOperation(value="获取用户详细信息", notes="根据id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
@RequestMapping(value = "user/query-by-id", method = RequestMethod.GET)
public Result<UserVo> queryUserById(@RequestParam Long id) {
return success(userManager.queryUserById(id));
}
}
显示的页面 http://localhost:8080/swagger-ui.html:
二、接入swagger-