Springdoc 无法检测 POJO 的字段以作为 UI 上的单个参数进行映射

问题描述

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

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

这里是 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 + "]";
}

}

这是 springdoc-ui 如何解释它以在 UI 中显示参数。

enter image description here

我没有在控制器中使用 @RequestBody 但 springdoc 假设输入是 JSON 正文的形式。我打算将它作为查询参数如下

enter image description here

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

解决方法

使用@ParameterObject 解决了这个问题。

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

在这里找到解决方案:

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

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