Search code examples
javaspring-bootswaggerspringfox

New endpoint causes IllegalStateException in test: Invalid mime type "headers": does not contain '/'


I have added a new endpoint to my Swagger v2 specification, that is being used in a Java 8 Spring-Boot application using SpringFox for code generation.

The code generation is successful and the app compiles, but the @RestController cannot start up. All tests and normal start up sequence fail with the following error message, even those unrelated to the new endpoint:

[ERROR] someUnitTest(com.mycompany.api.corporate.server.controller.SomeControllerTest) Time elapsed: 0 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/root/.m2/repository/io/springfox/springfox-spring-web/2.8.0/springfox-spring-web-2.8.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/root/.m2/repository/io/springfox/springfox-spring-web/2.8.0/springfox-spring-web-2.8.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Invalid mapping on handler class [com.mycompany.api.corporate.server.controller.OrderController]: public org.springframework.http.ResponseEntity com.mycompany.api.corporate.server.controller.OrderController.finishOrder(com.mycompany.api.corporate.server.model.CustomerData,java.lang.String)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/root/.m2/repository/io/springfox/springfox-spring-web/2.8.0/springfox-spring-web-2.8.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed;
nested exception is java.lang.IllegalStateException: Invalid mapping on handler class [com.mycompany.api.corporate.server.controller.OrderController]: public org.springframework.http.ResponseEntity com.mycompany.api.corporate.server.controller.OrderController.finishOrder(com.mycompany.api.corporate.server.model.CustomerData,java.lang.String)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Invalid mapping on handler class [com.mycompany.api.corporate.server.controller.OrderController]: public org.springframework.http.ResponseEntity com.mycompany.api.corporate.server.controller.OrderController.finishOrder(com.mycompany.api.corporate.server.model.CustomerData,java.lang.String)
Caused by: java.lang.IllegalStateException: Invalid mapping on handler class [com.mycompany.api.corporate.server.controller.OrderController]: public org.springframework.http.ResponseEntity com.mycompany.api.corporate.server.controller.OrderController.finishOrder(com.mycompany.api.corporate.server.model.CustomerData,java.lang.String)
Caused by: org.springframework.http.InvalidMediaTypeException: Invalid mime type "headers": does not contain '/'
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "headers": does not contain '/'

Given the trace, I think that the problem lies in this endpoint:

  /order/{id}/finish:
    post:
      tags:
      - Order
      summary: TODO
      description: TODO
      operationId: finishOrder
      produces:
      - application/json
      parameters:
      - in: path
        name: id
        required: true
        description: TODO
        type: string
      - in: body
        name: customerData
        required: true
        description: TODO
        schema:
          $ref: "#/definitions/CustomerData"
      responses:
        201:
          description: Account created
          schema:
            $ref: "#/definitions/StatusMessage"
          examples:
            application/json:
              code: "00000"
            headers:
              Location:
                type: string
                description: Resource location to be used for later
        400:
          description: >
            Failed to create the account.
            See error code and message in object.
          schema:
            $ref: "#/definitions/StatusMessage"
          examples:
            application/json:
              code: "31120"
              message: "Last name is too long"

Why do the code generation and build work but not the execution?


Solution

  • There is a syntax error in the specification, that is not being picked up by the Swagger Editor or the Codegen plugin:

          responses:
            201:
              description: Account created
              schema:
                $ref: "#/definitions/StatusMessage"
              examples:
                application/json:
                  code: "00000"
                headers:  # This is the problem: response headers should not be defined here
                  Location:
                    type: string
                    description: Resource location to be used for later
    

    The application interpreted headers in the first variant as a media type (which is what it expects at this place); a valid media type should have a / in the middle; this is exactly what the error message is saying.

    The correct way to declare response headers would be:

          responses:
            201:
              description: Account created
              schema:
                $ref: "#/definitions/StatusMessage"
              headers:
                Location:
                  type: string
                  description: Resource location to be used for later
              examples:
                application/json:
                  code: "00000"
    

    This way, the definition is correctly picked up and the response header appears in the generated HTML in the Swagger Editor.