SpringBoot 3.3.4中OpenAPI接口Swagger测试返回415错误求助
问题解决步骤
1. 修正OpenAPI YAML定义
确保api.yaml中/verify接口的form-urlencoded请求体符合OpenAPI 3.x规范,避免混用旧版语法:
paths: /verify: post: tags: - Verification summary: 验证VP Token requestBody: required: true content: application/x-www-form-urlencoded: schema: type: object properties: vp_token: type: string description: 需要验证的VP Token required: - vp_token responses: '200': description: 验证成功 content: application/json: schema: type: object properties: valid: type: boolean
2. 调整OpenAPI Generator Maven配置
将useRequestParamForFormData=true配置到globalProperties中,同时开启Spring Boot 3和Jakarta EE兼容:
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>7.9.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec> <generatorName>spring</generatorName> <apiPackage>com.yourproject.api</apiPackage> <modelPackage>com.yourproject.model</modelPackage> <globalProperties> <globalProperty>useRequestParamForFormData=true</globalProperty> <globalProperty>useJakartaEe=true</globalProperty> </globalProperties> <configOptions> <springBootVersion>3.3.4</springBootVersion> <useSpringBoot3>true</useSpringBoot3> <interfaceOnly>true</interfaceOnly> <!-- 按需开启,仅生成接口 --> <useJakartaValidation>true</useJakartaValidation> </configOptions> </configuration> </execution> </executions> </plugin>
执行mvn clean generate-sources重新生成代码,确认生成的VerificationApi接口中vp_token参数使用@RequestParam而非@RequestPart。
3. 修复Swagger UI自动设置Content-Type
如果Swagger UI仍未自动添加正确的请求头,可通过以下两种方式处理:
方式1:在YAML中显式声明consumes
paths: /verify: post: tags: - Verification summary: 验证VP Token consumes: - application/x-www-form-urlencoded requestBody: # 保留原有requestBody定义
方式2:自定义Swagger UI配置类
通过代码强制为该接口设置默认请求头:
import org.springdoc.core.customizers.OpenApiCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { @Bean public OpenApiCustomizer customOpenApi() { return openApi -> openApi.getPaths().get("/verify").getPost() .addExtension("x-swagger-ui-request-header", """ {"name": "Content-Type", "value": "application/x-www-form-urlencoded"} """); } }
4. 验证控制器实现
确保控制器实现类无注解冲突,正确使用@RequestParam:
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class VerificationController implements VerificationApi { @Override @PostMapping("/verify") public ResponseEntity<VerificationResponse> verify(@RequestParam("vp_token") String vpToken) { // 业务逻辑实现 return ResponseEntity.ok(new VerificationResponse(true)); } }
5. 清理并重新构建
执行命令确保生成代码和项目构建是最新的:
mvn clean install
启动应用后,访问Swagger UI测试/verify接口,此时应自动携带Content-Type: application/x-www-form-urlencoded头,参数以form-urlencoded格式发送,不再返回415错误。
内容的提问来源于stack exchange,提问作者Rosen Hristov
相关产品推荐
相关产品推荐

