SpringBoot微服务整合Swagger

单体项目的Swagger整合比较好整合,直接导入依赖即可,但是分布式或者微服务的项目整合就要稍微麻烦一点!
1.抽离出公共模块Common
提供给其他的模块使用,因为考虑到分布式和微服务有多个服务器,所以这里就同一抽离出来,让其他业务服务器都能使用到!
这个common创建一个Maven就行了

2.pom导入Swagger2的依赖


        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,针对您的问题,我可以给出以下步骤: 1. 在 pom.xml 文件中添加 Swagger2 和 Swagger UI 的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> ``` 2. 创建 Swagger 配置类,用于配置 Swagger 的基本信息和扫描的包路径: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 3 整合 Swagger") .description("使用 Swagger 构建 API 文档") .version("1.0.0") .build(); } } ``` 3. 在 Controller 类中添加 Swagger 注解,用于描述接口信息: ```java @RestController @RequestMapping("/user") @Api(tags = "用户管理") public class UserController { @GetMapping("/{id}") @ApiOperation(value = "根据 ID 获取用户信息", notes = "根据传入的 ID 参数获取用户信息") @ApiImplicitParam(name = "id", value = "用户 ID", required = true, dataType = "Long", paramType = "path") public User getUserById(@PathVariable Long id) { // 根据 ID 查询用户信息 return userService.getUserById(id); } // 其他接口方法... } ``` 4. 启动应用程序,访问 http://localhost:8080/swagger-ui.html 即可查看生成的 API 文档。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员劝退师-TAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值