简体   繁体   English

springdoc-openapi、swagger UI 中的 XML 示例

[英]XML examples in springdoc-openapi, swagger UI

I'm struggling with generation of correct XML examples in springdoc-openapi library.我正在努力在springdoc-openapi库中生成正确的 XML 示例。

Controller: Controller:

@RestController
@RequestMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public class MyController {
    @GetMapping
    public MonetaryValue getMonetaryValue() {
        return new MonetaryValue(BigDecimal.ONE, "EUR");
    }
}

Model Model

@Schema(name="price", description = "Monetary value")
@JacksonXmlRootElement(localName = "price")
public class MonetaryValue {
    @Schema(example = "EUR")
    @JsonProperty("cur")
    @JacksonXmlProperty(localName = "cur", isAttribute = true)
    private String currency;
    
    @Schema(example = "1.00")
    @JsonProperty("val")
    @JacksonXmlText
    private BigDecimal value;
    
    // getters, setter ...
}

End point called with header Accept: application/xml returns expected result:使用 header 调用的端点Accept: application/xml返回预期结果:

<price cur="EUR">1</price>

However it seems that Jackson annotations are ignored in springdoc.但是,springdoc 中似乎忽略了 Jackson 注释。 Example shown in swagger-ui shows: swagger-ui 中显示的示例显示:

<value>
    <cur>EUR</cur>
    <val>1</val>
</value>

I have also tried to add JAXB annotations.我还尝试添加 JAXB 注释。

@Schema(name="price", description = "Monetary value")
@JacksonXmlRootElement(localName = "price")
public class MonetaryValue {
        @Schema(example = "EUR")
    @JsonProperty("cur")
    @JacksonXmlProperty(localName = "cur", isAttribute = true)
    @XmlAttribute(name = "cur")
    private String currency;
    
    @Schema(example = "1.00")
    @JsonProperty("val")
    @JacksonXmlText
    @XmlValue
    private BigDecimal value;   
    // getters, setter ...
}

It partly helped, attribute is shown correctly, but @XmlValue annotation seems to be is ignored.它在一定程度上有所帮助,属性显示正确,但@XmlValue注释似乎被忽略了。 Value is shown in sub-element val instead of element's text.值显示在子元素val中,而不是元素的文本中。

<value cur="EUR">
    <val>1</val>
</value>

JSON examples work without any problem. JSON 示例可以正常工作。

Demo project on GitHub . GitHub 上的演示项目

Thanks for help.感谢帮助。

This is an issue with the OpenAPI spec.这是 OpenAPI 规范的一个问题。

<value cur="EUR">1</value>

The XML structure you want to achieve is not supported by OpenAPI. OpenAPI不支持你想要实现的XML结构。

There is a GitHub issue tracking this: https://github.com/OAI/OpenAPI-Specification/issues/630有一个跟踪此问题的 GitHub 问题: https://github.com/OAI/OpenAPI-Specification/issues/630

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 springdoc-openapi 不同的例子 - springdoc-openapi different examples Spring springdoc-openapi:swagger-ui/index.html 找不到状态=404 - Spring springdoc-openapi : swagger-ui/index.html cannot be found status=404 UnitTest 调用 /swagger-ui.html 导致 springdoc-openapi 404 - UnitTest call /swagger-ui.html results in a 404 for springdoc-openapi Java springdoc-openapi 在 Swagger UI 示例值中显示带有附加日期/时间字段的 LocalDateTime 字段 - Java springdoc-openapi show LocalDateTime field with additional date/time fields in Swagger UI Example Value springdoc-openapi swagger 不要带参数调用 - springdoc-openapi swagger don't make calls with parameters 如何使用带有 Lombok getter 的 springdoc-openapi 将 @JsonValue 用于 Swagger 枚举值 - How can @JsonValue be used for Swagger enum values using springdoc-openapi with a Lombok getter Springdoc-OpenAPI 带路径参数和命令 object - Springdoc-OpenAPI with path parameters and command object 带有 springdoc-openapi-ui 的 swagger-codegen-maven-plugin - swagger-codegen-maven-plugin with springdoc-openapi-ui springdoc-openapi 通用控制器类型参数支持 - springdoc-openapi generic controller type parameters support 如何为 springdoc-openapi 端点调用添加 Header 授权 - How to add Header with Authorization for springdoc-openapi endpoint calls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM