Springdoc 无法检测 POJO 的字段以映射为 UI 上的单独参数

use*_*004 3 spring-boot springfox springdoc

我的 springboot 应用程序有一个 @RestController ,它采用 POJO 类作为参数。

@GetMapping(path="/")
public void sayHello(Person person) {
    System.out.println(person);
}
Run Code Online (Sandbox Code Playgroud)

这是 Person 类的定义,它只是一个 POJO。

public class Person {

private String firstName;
private String lastName;
private int age;

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

@Override
public String toString() {
    return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
}
Run Code Online (Sandbox Code Playgroud)

}

这就是 springdoc-ui 解释它在 UI 中显示参数的方式。

在此输入图像描述

我没有在控制器中使用 @RequestBody,但 springdoc 假定输入采用 JSON 正文的形式。我打算将其作为查询参数,如下所示

在此输入图像描述

有趣的是,如果我将 swagger 实现更改为 springfox,默认情况下 POJO 的每个字段都会被解释为 UI 中的单独参数。最后一张截图是使用 springfox 实现拍摄的。如何使用 springdoc 获得相同的行为?

use*_*004 5

使用 @ParameterObject 可以解决这个问题。

@GetMapping(path="/")
public void sayHello(@ParameterObject Person person) {
    System.out.println(person);
}
Run Code Online (Sandbox Code Playgroud)

在这里找到了解决方案:

https://github.com/springdoc/springdoc-openapi/issues/162

https://github.com/springdoc/springdoc-openapi/pull/505