我能够将Springdoc与Springdoc OpenAPI集成在一个小型应用程序中,该应用程序具有面向微服务的体系结构。但是现在,当我打开Swagger来测试我的控制器(StudentController)的端点时,我意识到控制器根级的@RequestMapping并不包含在要测试的端点的URL中。路线的定义如下:
routes:
- id: students-service
uri: lb://STUDENTS-SERVICE
predicates:
- Path=/students/**
filters:
- RewritePath=/students/(?<path>.*), /$\{path}
- id: openapi
uri: http://localhost:${server.port}
predicates:
- Path=/v3/api-docs/**
filters:
- RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/v3/api-docs StudentController是这样的:
@RestController
@RequestMapping("/api/v1")
@Tag(name = "Students")
public class StudentQueryController {
@GetMapping("/message")
public String dummyGet() {
return "Student Service Dummy GET";
}
}在Swagger中,我希望得到像"http://localhost:8060/students/api/v1/message"“这样的URL,但是我得到了"http://localhost:8060/api/v1/message"”。
有人能帮我解决这个问题吗?提前谢谢。
发布于 2021-06-30 04:12:11
如果在像这样的代理后面的应用程序中配置了springdoc,那么您应该在服务应用程序中配置这个属性:
server.forward-headers-strategy=framework
这个特性是在spring 2.2.0中引入的。
解释:
当请求通过Spring时,一些与代理相关的头将被添加到请求中(如x-forwarded-host、x-forwarded-proto等)。
设置此属性后,应用程序将使用这些标头进行正确的重定向。它不是重定向到/api/v1/message,而是重定向到/students/api/v1/message (因为/students前缀将在x-forwarded-prefix头中可用)。
只有当所有应用程序后面的代理正确配置这些标头并且您可以信任该代理时,才使用此设置。 如果您将所有应用程序限制为只能通过访问,那么您就可以了。
您可以阅读有关此参数和行为这里的更多信息。
https://stackoverflow.com/questions/66953605
复制相似问题