Search code examples
spring-bootspringfoxspringdoc

Springdoc cannot detect POJO's fields to map as individual parameters on UI


My springboot application has a @RestController which takes a POJO class as parameter.

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

and here's the definition of Person class which is just a 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 + "]";
}

}

This is how springdoc-ui interprets it for showing parameters in UI.

enter image description here

I did not use @RequestBody in the controller yet springdoc assumes the input to be in the form of JSON body. I intend it to be as query parameters as below

enter image description here

Interestingly if I change the swagger implementation to springfox, each fields of the POJO is interpreted as individual parameters in the UI by default. The last screenshot was taken using springfox implemntation. How do I get the same behavior with springdoc?


Solution

  • Using @ParameterObject fixes this.

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

    Found the solution here:

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

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