您好,以下是springboot整合swagger2的步骤demo:
- 在pom.xml文件中添加swagger2的依赖:
<dependency> <groupId>io.<em>spring</em>fox</groupId> <artifactId><em>spring</em>fox-<em>swagger</em>2</artifactId> <version>2.9.2</version> </dependency>
讯享网
- 创建Swagger2配置类,用于配置Swagger2相关信息:
讯享网@Configuration @Enable<em>Swagger</em>2 public class <em>Swagger</em>2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.<em>SWAGGER</em>_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.<em>demo</em>.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("<em>Spring</em> <em>Boot</em>中使用<em>Swagger</em>2构建RESTful APIs") .description("更多<em>Spring</em> <em>Boot</em>相关文章请关注:http://www.example.com/") .termsOfServiceUrl("http://www.example.com/") .contact("example") .version("1.0") .build(); } }
- 在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) { // ... } }
- 启动应用程序,访问http://localhost:8080/swagger-ui.html,即可查看Swagger2生成的接口文档。
希望对您有所帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/190052.html