Spring webflux doesn't populate custom object from path variables with @ModelAttribute

Viewed 867

I'm trying to adapt an application that works well with Spring MVC but that has a different behavior with Spring WebFlux

Here's my code with Spring Boot 5 - Spring MVC :

The controller :

@RestController
public class MyRestController {

    @GetMapping("/test/{id}/{label}")
    public ResponseEntity<Payload> test(@ModelAttribute Payload payload) {
        return new ResponseEntity<>(payload,HttpStatus.OK);
    }
}

The Payload object :

public class Payload {

    @NotNull
    private int id;

    private String label;

    public Payload() {}

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

My pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

I don't have written any custom converter, Spring populate my payload object automatically, everything is fine.

When I call :

http://localhost:8080/test/25/helloWorld

Response is

{"id":25,"label":"helloWorld"}

Then, I only change my pom.xml, switching from web to webflux :

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
</dependencies>

And my Payload Object isn't populated anymore.

When I call :

http://localhost:8080/test/25/helloWorld

Response is

{"id":0,"label":null}

I know I could write a converter and register it with @ControllerAdvice, but I can't imagine that there isn't an automated solution to make it works again as it has always worked with Spring Web.

Does anyone already encountered the same problem as me ?

Thanks,

Julien

1 Answers

The Spring WebFlux reference documentation about @ModelAttribute does not mention URI path variables, unlike the same section in the reference documentation for Spring MVC:

The Pet instance above is resolved as follows:

  • From the model if already added by using Model.
  • From the HTTP session by using @SessionAttributes.
  • From a URI path variable passed through a Converter (see the next example).
  • From the invocation of a default constructor.
  • From the invocation of a “primary constructor” with arguments that match to Servlet request parameters. Argument names are determined through JavaBeans @ConstructorProperties or through runtime-retained parameter names in the bytecode.

At this point this is the expected behavior and there might be good reasons or limitations behind that choice. Feel free to open an enhancement request in Spring Framework for that.

Related