gpt4 book ai didi

java - 新端点在测试中导致 IllegalStateException : Invalid mime type "headers": does not contain '/'

转载 作者:行者123 更新时间:2023-12-01 21:36:34 25 4
gpt4 key购买 nike

我在 Swagger v2 规范中添加了一个新端点,该端点正在使用 SpringFox 进行代码生成的 Java 8 Spring-Boot 应用程序中使用。

代码生成成功并且应用程序编译,但@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"

为什么代码生成和构建可以工作,但执行却不能?

最佳答案

规范中存在语法错误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 解释为媒体类型(这是它在此位置所期望的);有效的媒体类型中间应有一个 /;这正是错误消息所说的内容。

声明响应 header 的正确方法是:

      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"

这样,就可以正确获取定义,并且响应 header 会出现在 Swagger 编辑器生成的 HTML 中。

关于java - 新端点在测试中导致 IllegalStateException : Invalid mime type "headers": does not contain '/' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58801547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com