为什么我无法在Swagger UI中为我使用Sping Boot 创建的端点自定义请求主体?

k4ymrczo  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(217)

我已经创建了一个端点,它允许您通过id获取图书的详细信息。

@ResponseBody
@RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
public BookResponse getDetails(
                    @PathVariable(name = "id", required = true) int id,
                    @PathVariable(name = "name", required = false) String name,
                    @RequestBody BookRequest request)
{
    return controllerBook.getBookDetails(request, id, name);
}

请求正文为

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonIgnoreProperties(ignoreUnknown = true)
@ApiModel("BookRequest")
public class BookRequest {
    @NotNull(message = "Id of user created while registration")
    @ApiModelProperty(value = "Id of user created while registration")
    private int userId;
    @NotNull(message = "Id of book")
    @ApiModelProperty(value = "Id of book")
    private int bookId;
    @NotNull
    @ApiModelProperty(value = "Detail of book")
    private String detail1;
    @ApiModelProperty(value = "Detail of book")
    private String detail2;
    @ApiModelProperty(value = "Name of book")
    private String name;
    @ApiModelProperty(value = "Author of book")
    private String author;
    @ApiModelProperty(value = "Category of book")
    private String category;
    @ApiModelProperty(value = "Language of book")
    private String language;
}

我尝试使用@Api而不是@ApiModel,并尝试将参数的关键字更改为example,name in @ApiModelProperty,还尝试使用不带关键字的参数。
我使用的是springdoc openapi用户界面

<dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.4</version>
        </dependency>

每次运行应用程序并打开swagger UI时,我都无法查看请求正文中的这些更改,只能看到默认值。

我是否遗漏了什么?或者我是否使用了@ApiModelProperty来做不应该做的事情?如果是这样的话,在这里应该使用什么正确的 Package 器或注解?如果有人澄清这一点,这将是非常有帮助的。
问题已解决。请检查pan-leszeczek发布的答案和相应的线程。

5vf7fwbs

5vf7fwbs1#

您需要使用该注解的“example”属性:

@ApiModelProperty(example = "example value of property")

正如注解中提到的,ApiModelProperty -〉example在使用springfox依赖项的情况下可以正常工作,但由于使用了springdoc-openapi-ui,因此应该使用

@Schema(... example = "example value")

在请求/响应模型中

ioekq8ef

ioekq8ef2#

请求模型文档应该在swagger ui页面的另一个名为“Schemas”的部分中,如果定义正确,向下滚动swagger ui页面,它应该在下面。
编辑:或者您可以通过按下“示例值”右侧的“模式”选项卡来查看它

相关问题