【问题标题】:How to annotate request body to describe examples如何注释请求正文以描述示例
【发布时间】:2021-04-11 20:30:52
【问题描述】:

我正在努力正确描述 requestBody。 我有这个 Dto 作为请求正文:

public @Data class ContactDto {
@Parameter(description = "Mailadress required if messageType is MAIL")
private String mailAddress;
@Parameter(description = "Phonenumber required if messageType is not MAIL", example = 
"0041791234567")
private String phoneNumber;
@Parameter(description = "Message type which will be used to inform the user", examples = {
        @ExampleObject(name = "SMS", value = "SMS"),
        @ExampleObject(name = "MAIL", value = "MAIL")
})
private MessageType messageType;

}

这在控制器中:

@PostMapping(consumes = "application/json")
public ResponseEntity<Object> createWichtel(@RequestBody() final WichtelDetailsDto wichtelDetailsDto) 
{
    return new ResponseEntity<>(HttpStatus.CREATED);
}

我正在使用 Spring 和 springdoc-openapi-ui

但是当我打开 swagger-ui 时,上面的描述没有显示。 这里有什么错误?

【问题讨论】:

    标签: spring annotations swagger


    【解决方案1】:

    只需使用@ApiParam

    public @Data class ContactDto {
    @ApiParam(value = "Mailadress required if messageType is MAIL")
    private String mailAddress;
    @ApiParam(value = "Phonenumber required if messageType is not MAIL", example = 
    "0041791234567")
    private String phoneNumber;
    @ApiParam(value = "Message type which will be used to inform the user", example = "{(name = \"SMS\", value = \"SMS\")}")
    private MessageType messageType;
    

    【讨论】:

    • @ApiParam 未知。我需要添加另一个依赖项吗?
    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 2022-12-30
    • 2021-08-12
    • 2020-01-13
    • 1970-01-01
    • 2013-07-15
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多