Skip to content

Controller method argument initialized either from the request body or from request parameters [SPR-13742] #18315

Closed
@spring-projects-issues

Description

@spring-projects-issues

Rossen Stoyanchev opened SPR-13742 and commented

@RequestBody converts the body of the request to an object while @ModelAttribute binds request parameters to an object. The former is common with REST APIs while the latter is common with HTML forms in browser applications.

JavaScript UI components however may send HTTP GET requests via Ajax with data represented either as JSON input in the body or appended as query parameters. See relevant discussion under SPR-13679. It would be useful to have a single controller method argument that supports both.

One way to do this could be to delegate to either @RequestBody or @ModelAttribute argument resolution. However for the given use case it may make more sense to somehow fold this together with @RequestBody handling such that we either read the body or treat request parameters as form data. This is something we already do for HTTP POST albeit for a different reason (i.e. to avoid issues where a call to getParameter from a Filter causes the request body to be consumed before we try to read it). This would also be a more consistent and simpler to understand approach, effectively it can still be thought of as @RequestBody handling. It would also be suited for the given use case, i.e. Ajax requests where for example a 400 response is perfectly okay for error responses (vs rendering an HTML error page).


Affects: 4.2.3

Issue Links:

Referenced from: commits spring-attic/spring-framework-issues@cdde05b

2 votes, 8 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions