微服务下统一管理配置swagger

本文介绍了在微服务架构下如何通过创建一个独立模块来集中管理Swagger接口文档,包括创建管理模块、配置Swagger、编写接口、继承并实现接口、配置启动类以及访问Swagger UI的方法。在微服务中正确配置Swagger有助于提升接口文档的管理和维护效率。

优快云停更,所有博客已搬至 rainofshadow.top 个人服务器。 最新文章,移至 https://rainofshadow.top/

 

最近武汉瘟疫挺严重的,学校被封了.....在家完全没什么状态,感觉整个人都废了...昨天开始动工,记录一下微服务如何配置swagger和遇到的问题。

1.在工程下创建一个模块,用来统一管理swagger的api

没有启动类,在其他服务的pom中要依赖api这个模块。

swagger的maven依赖

  <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
     </dependency>
  <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
          <version>2.8.0</version>
   </dependency>

2.配置Swagger


@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.recommend"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("个性化学习推荐系统api文档")
                .description("个性化学习推荐系统api文档")
//                .termsOfServiceUrl("http://localhost:8502/")
                .version("1.0")
                .build();
    }
}

 

3.在api模块中写各个微服务的接口

举个例子


@Api(value = "管理员微服务",description = "发布试题与岗位公司")
public interface ManagerWrittenExaminationControllerApi {

    @ApiOperation("上传试题")
    @ApiImplicitParams({
            @ApiImplicitParam(name="file",value = "excel文件",required=true,paramType="file"),
    })
    public Result uploadProblems(MultipartFile file);

    @ApiOperation("修改题")
    public Result updateProblemById(Problem p);


    @ApiOperation("根据Id删除题")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "题id",required=true,paramType="path",dataType="Integer")
    })
    public Result deleteProblemById(Integer id);


    @ApiOperation("添加岗位")
    public Result addPosition(Position position);

    @ApiOperation("删除岗位")
    public Result deletePosition(Integer id);

    @ApiOperation("添加公司")
    public Result addCompany(Company company);

    @ApiOperation("删除公司")
    public Result deleteCompany(Integer id);
}

4.在其他的微服务controller继承api接口

然后实现对应的方法,值得注意的是,没有写方法的请求路径,swagger-ui访问后是什么也没有的。

 

5.微服务启动类的配置

一定要扫描到   api 模块中 api下的接口 ,和自身的controller层的接口,不然swagger无法访问之类的问题

@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages={"com.recommend.service.api"})
@ComponentScan(basePackages={"com.recommend.service.written"})
@MapperScan(basePackages = {"com.recommend.common.mapper", "com.recommend.service.written.mapper"})
public class ServiceWrittenApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceWrittenApplication.class);
    }
    @Bean
    public JwtUtil jwtUtil(){return  new JwtUtil();}
}

6.启动微服务访问swagger-ui页面

本地就 localhost:  端口号/swagger-ui.html,

不同的微服务接口就改变不同的端口号就行.

### 配置和集成Swagger以实现API文档自动生成 在微服务架构中,为了有效地管理和维护各个服务之间的交互,使用Swagger可以极大地简化这一过程。通过配置Swagger,在开发环境中能够轻松地生成并查看API文档。 对于Java后端项目而言,通常会采用Spring Boot框架配合Swagger来完成这项工作。具体来说,可以通过引入`springfox-swagger2`以及`springfox-swagger-ui`这两个依赖项来快速搭建起基本的支持环境[^1]: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>${swagger.version}</version> </dependency> ``` 上述代码片段展示了如何在项目的POM文件中添加必要的Maven依赖,以便支持Swagger功能。其中`${swagger.version}`应替换为实际使用的Swagger版本号。 接着,为了让应用程序识别这些设置,还需要创建相应的配置类。下面是一个简单的示例,用于启动Swagger UI界面,并指定扫描路径以发现所有的控制器方法: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.basePackage("com.example")) .paths(PathSelectors.any()) .build(); } } ``` 这段代码定义了一个名为`api()`的方法,该方法返回一个Docket实例,这是Swagger的核心组件之一。这里指定了要扫描的基础包名(即`com.example`),并且选择了所有路径作为目标范围。这意呸着任何位于此基础包内的RESTful接口都将被纳入到最终生成的API文档之中。 当涉及到多个微服务时,则可能需要考虑如何集中管理各服务间的API文档。此时,可以利用网关层来进行统一处理——只需确保每个独立的服务都已正确设置了Swagger,并且能够在各自的上下文中正常运行;之后便可以在网关处实施聚合操作,从而让用户仅需访问单一入口就能浏览整个系统的全部公开接口信息[^3]。 此外,如果希望进一步提升用户体验,还可以尝试引入像Knife4j这样的增强型插件。这类工具不仅继承了原生Swagger的优势特性,还额外提供了诸如分组显示、在线调试等功能,有助于提高团队协作效率及降低沟通成本[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值