Spring中springdoc生成OpenAPI时方法级produces未覆盖类级问题
Spring Boot + springdoc 类级与方法级produces声明的OpenAPI生成异常
使用Spring Boot 3.5.0搭配springdoc 2.8.9时,在@RestController中同时指定类级和方法级produces属性时出现不符合预期的情况。
根据org.springframework.web.bind.annotation.RequestMapping#produces的Javadoc说明:
支持在类型级别和方法级别指定!若同时指定,方法级的produces条件会覆盖类型级条件。
但在如下示例控制器中:
@RestController @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) public class MediaTypeDemoController { @GetMapping("/json") public ResponseEntity<Map<String, String>> getJsonResponse() { Map<String, String> response = Map.of("message", "This is JSON"); return ResponseEntity.ok(response); } @GetMapping(value = "/xml", produces = MediaType.APPLICATION_XML_VALUE) public ResponseEntity<String> getXmlResponse() { String xml = "<message>This is XML</message>"; return ResponseEntity.ok(xml); } }
生成的OpenAPI规范里,GET /xml接口同时包含application/json和application/xml作为响应媒体类型,而预期应该仅显示方法级声明的application/xml。
更新:
该问题已通过springdoc的PR #3026修复。
内容的提问来源于stack exchange,提问作者Nicklas
相关产品推荐
相关产品推荐

