您好,以下是
springboot 整合 swagger2的步骤
demo:
swagger2的依赖:
<dependency>
<groupId>io.
springfox</groupId>
<artifactId>
springfox-
swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2. 创建
Swagger2
配置类,用于
配置 Swagger2相关信息:
@Configuration
@Enable
Swagger2
public class
Swagger2Config {
@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("
Spring Boot中使用
Swagger2构建RESTful APIs")
.description("更多
Spring Boot相关文章请关注:http://www.example.com/")
.termsOfServiceUrl("http://www.example.com/")
.contact("example")
.version("1.0")
.build();
}
}
3. 在Controller类中添加
Swagger2注解,用于描述接口信息:
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
@ApiOperation(value = "获取用户列表", notes = "获取所有用户列表")
@GetMapping("/list")
public List<User> list() {
// ...
}
@ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
@PostMapping("/create")
public String create(@RequestBody User user) {
// ...
}
@ApiOperation(value = "更新用户", notes = "根据User对象更新用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
})
@PutMapping("/update/{id}")
public String update(@PathVariable Long id, @RequestBody User user) {
// ...
}
@ApiOperation(value = "删除用户", notes = "根据ID删除用户")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable Long id) {
// ...
}
}
4. 启动应用程序,访问http://localhost:8080/
swagger-ui.html,即可查看
Swagger2生成的接口文档。
希望对您有所帮助!
到此这篇swagger2配置(swagger2配置登录)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/rfx/21943.html