[英]Spring REST API And OpenApi - Duplicated Context Path Call
I am creating a Spring RESTful API with an openapi starting point.我正在创建一个带有 openapi 起点的 Spring RESTful API。 In my openapi I defined the servers property to the localhost (with the port number and the context-path that I'm planning to use) and in my
application.yml
I defined the context path under the property server.servlet.context-path
, below are snippets of those two files.在我的 openapi 中,我将服务器属性定义为本地主机(使用端口号和我计划使用的上下文路径),在我的
application.yml
中,我在属性server.servlet.context-path
下定义了上下文路径,下面是这两个文件的片段。
my-api-v1.yaml
: my-api-v1.yaml
:
openapi: 3.0.3
info:
title: My API
description: The application exposes the my API endpoints to be used using RESTful services.
version: 0.1.0
servers:
- url: http://localhost:9001/my-api/v1
description: Local URL for my RESTful endpoints
tags:
- name: Users
description: Users endpoints
paths:
/users:
get:
tags:
- Users
summary: Get all users
description: Retrieve all the users from the database
operationId: getAllUsers
responses:
200:
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/UserDetail'
minItems: 0
maxItems: 20
components:
schemas:
UserDetail:
type: object
properties:
id:
description: User id
type: integer
format: int64
example: 1
firstName:
description: User's first name
type: string
example: John
lastName:
description: User's last name
type: string
example: Doe
application.yml
: application.yml
:
logging:
level:
root: INFO
org.springframework.web: ERROR
org.hibernate: ERROR
org.my.api: DEBUG
spring:
application:
name: My API
output:
ansi:
enabled: always
server:
address: localhost
port: 9001
servlet:
context-path: /my-api/v1
When I run my application, I can't call the users endpoint with what I expect the url would be (that is http://localhost:9001/my-api/v1/users
), I get a 400 error back.当我运行我的应用程序时,我无法使用我期望的 url 调用用户端点(即
http://localhost:9001/my-api/v1/users
),我收到 400 错误。
But if I write the context path twice, it works.但是,如果我将上下文路径写两次,它就可以工作。
If I comment one of them and re-run the application, then I can call the url with the context path written once.如果我评论其中一个并重新运行应用程序,那么我可以调用 url 并写入一次上下文路径。 Why is this happening?
为什么会这样? I am using openapi generator maven plugin to generate the controller and model from my openapi file (with generatorName spring).
我正在使用 openapi 生成器 maven 插件从我的 openapi 文件(带有 generatorName spring)生成 controller 和 model。 I checked both openapi generator and spring generator documentations to check if there is something or a property I can use to override the url to use but did not find anything.
我检查了 openapi 生成器和 spring 生成器文档,以检查是否有可以用来覆盖 url 的东西或属性,但没有找到任何东西。 How can I fix this so that I can write the context path once in the url and be able to use Try it out button when opening it in swagger-ui.
我该如何解决这个问题,以便我可以在 url 中编写一次上下文路径,并且能够在 swagger-ui 中打开它时使用试用按钮。
I could avoid the situation by changing the my-api-v1.yaml
like this:我可以通过像这样更改
my-api-v1.yaml
来避免这种情况:
servers:
- url: http://localhost:9001
description: Local URL for my RESTful endpoints
- url: http://localhost:9001/my-api/v1
description: Local URL for my RESTful endpoints
First line of servers should be without path in order not to duplicate.第一行服务器应该没有路径,以免重复。
You can test with second one.你可以用第二个来测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.