'How to add basepath in OpenApi so that it is autogenerated with maven?

I have a springboot project in which I have developed an api with OpenApi in yml format and autogenerated the classes with openapi-generator-maven-plugin. The yml is as follows:

openapi: 3.0.2
info:
  version: 0.0.1-SNAPSHOT
  title: Example API
servers:
  - description: Localhost
    url: 'http://localhost:{port}/my-first-api'
    variables:
      port:
        default: '8080'

tags:
  - name: Example

paths:
  /api/v1/examples:
    get:
      summary: Get examples
      operationId: getExamples
      description: Obtain a list of available examples.
      tags:
        - Example
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Example'

components:
  schemas:
    Example:
      title: Example
      type: object
      properties:
        description:
          type: string
        check:
          type: boolean
      example:
        description: 'Example'
        check: true

enter image description here

As you can see, I have defined that the local base path is:

http://localhost:8080/my-first-api

And later for the only available endpoint that is added:

/api/v1/examples

Therefore, I expected that once the artifact was started locally, I could consume the endpoint from this URL:

http://localhost:8080/my-first-api/api/v1/examples

But my surprise is that it doesn't work, this URL is not found. But if it finds the following:

http://localhost:8080/api/v1/examples

As you can see, it accesses without the "my-first-api" part of the path, but I need this part of the path to be there too... What could be happening?

Thanks!



Solution 1:[1]

In my tests, it worked just fine. The my-path part got changed, matching the spec changes.

@RequestMapping("${project.name.base-path:/my-path}")

But as you can see, spring would allow you to override this base URL using the project.name.base-path property. (The actual property name is probably different for you)

So, my suggestion would be:

  1. Check if the annotation on the generated Controller changes at all.
  2. If it does, check if the property is overridden at some point.
  3. Check if you are setting spring's own base URL with the property server.servlet.context-path

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 3Fish