我在Swagger v2规范中添加了一个新端点,该端点已在Java 8 Spring-Boot应用程序中使用SpringFox进行代码生成。
代码生成成功并且可以编译应用程序,但是@RestController
无法启动。所有测试和正常的启动顺序都会失败,并显示以下错误消息,即使是与新端点无关的错误消息:
[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 '/'
考虑到跟踪,我认为问题出在这个端点上:
/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"
为什么代码生成和构建工作而不执行工作?
答案 0 :(得分:0)
规范中存在一个语法错误,Swagger Editor或Codegen插件未捕获该错误:
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
应用程序在第一个变体中将headers
解释为一种媒体类型(这是它在此位置所期望的);有效的媒体类型中间应有/
;这正是错误消息的意思。
声明响应标头的正确方式为:
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"
通过这种方式,可以正确提取定义,并且响应标头会显示在Swagger编辑器中生成的HTML中。