I am creating a Spring RESTful API with an openapi starting point. 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.
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
:
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.
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. Why is this happening? I am using openapi generator maven plugin to generate the controller and model from my openapi file (with generatorName spring). 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. 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.
I could avoid the situation by changing the my-api-v1.yaml
like this:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.