简体   繁体   English

Swagger与Spring Boot微服务

[英]Swagger with spring boot microservice

I have a microservice-A which gets the token as a header from another microservice-B . 我有一个微服务A ,它从另一个微服务B获得令牌作为头。 Now I want to implement swagger2 in microservice-A . 现在,我想在microservice-A中实现swagger2 The problem is every request flows through microservice-B . 问题是每个请求都流经microservice-B So swagger-ui throws error in local as 所以swagger-ui在本地抛出错误

it is not able to get those header parameter which microservice-B is trying to fetch. 它无法获得微服务B试图获取的标头参数。

It is not able to get those header parameter which microservice-B is trying to fetch. 无法获得微服务B试图获取的标头参数。

Swagger on its own can't call the authenticator service and add the obtained token to another request's header. Swagger本身无法调用身份验证器服务并将获得的令牌添加到另一个请求的标头中。

You can modify the Docket object to accept additional parameter in header as below: 您可以修改Docket对象以接受标头中的其他参数,如下所示:

docket.globalOperationParameters(
    Collections.singletonList(new ParameterBuilder()
      .name("Authorization")
      .description("Bearer [token]")
      .modelRef(new ModelRef("string"))
      .parameterType("string")
      .required(true)
      .build()
    )
);

This will allow Swagger UI to show additional field to accept token (see image below). 这将允许Swagger UI显示其他字段以接受令牌(请参见下图)。 You need to obtain the token by yourself and can put in this field. 您需要自己获取令牌,并可以在此字段中输入。

在此处输入图片说明 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM