Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document feature to load @ModelAttribute through type conversion from a request value #26873

Closed
aoyvx opened this issue Apr 29, 2021 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: documentation A documentation task
Milestone

Comments

@aoyvx
Copy link
Contributor

aoyvx commented Apr 29, 2021

The behavior of @ModelAttribute in Web MVC and WebFlux is different.

Except for #26856, the HTTP GET parameter cannot be bound normally.

In WebFlux and Web MVC 5.3.4 :

@RestController
@SpringBootApplication
public class MyApp  {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class);
    }

    @GetMapping("/echo")
    public String echo(@ModelAttribute("msg") String msg) {
        return msg;
    }
}

when I call http://localhost:8080/echo?msg=hello, I get `` for WebFlux but hello for Web MVC.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 29, 2021
@sbrannen
Copy link
Member

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

@sbrannen
Copy link
Member

Out of curiosity, why are you using @ModelAttribute for a String?

@sbrannen sbrannen added in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-feedback We need additional information before we can continue labels Apr 29, 2021
@sbrannen sbrannen changed the title @Modelattribute in webflux cannot be binding HTTP GET param @ModelAttribute in WebFlux does not bind an HTTP GET parameter Apr 29, 2021
@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 29, 2021
@aoyvx
Copy link
Contributor Author

aoyvx commented Apr 29, 2021

Out of curiosity, why are you using @ModelAttribute for a String?

I didn't use @ModelAttribute on string in my work.It's just that when I was learning spring, I found that webflux and webmvc have some behavioral differences.Webmvc will handle UrlParam, but Webflux will not, so this question was raised.
This is the first time I put forward issues in GitHub. Maybe it's bad. If it puzzles you, I'm sorry.

@sbrannen sbrannen changed the title @ModelAttribute in WebFlux does not bind an HTTP GET parameter @ModelAttribute in WebFlux does not bind an HTTP query parameter Apr 29, 2021
@sbrannen
Copy link
Member

I didn't use @ModelAttribute on string in my work.It's just that when I was learning spring, I found that webflux and webmvc have some behavioral differences.Webmvc will handle UrlParam, but Webflux will not, so this question was raised.

Thanks for providing the explanation.

This is the first time I put forward issues in GitHub.

Indeed it is... your first GitHub issue ever. Congratulations!

Maybe it's bad. If it puzzles you, I'm sorry.

Oh, no. It's not bad at all. I was merely wondering what your use case was for using @ModelAttribute with a simple type like a String as opposed to a custom type like an Order, User, etc.

In any case, we will investigate the cause for the different behavior.

@rstoyanchev
Copy link
Contributor

@aoyvx, this is an extra feature in Spring MVC where the model attribute can be loaded through a converter. A typical example might look like this:

@PostMapping("/{account}")
public String update(@Valid @ModelAttribute("account") Account account, BindingResult result) {
   // ...
}

Note that the model attribute name "account" matches the name of a request value (in this case a path variable). When this is the case and there is a Converter from String to Account, it is used to load the model attribute rather than instantiating it. The same also works for request parameters as in your case.

I have realized that this not documented in the reference docs, which we can address. The same feature currently does not exist in WebFlux.

@rstoyanchev rstoyanchev self-assigned this May 4, 2021
@rstoyanchev rstoyanchev added type: documentation A documentation task and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on labels May 4, 2021
@rstoyanchev rstoyanchev added this to the 5.3.7 milestone May 4, 2021
@rstoyanchev rstoyanchev changed the title @ModelAttribute in WebFlux does not bind an HTTP query parameter Document feature to load @ModelAttribute through type conversion from a request value May 5, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants