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

@RestController avoid declare @RequestBody too [SPR-13868] #18441

Closed
spring-issuemaster opened this Issue Jan 13, 2016 · 2 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Jan 13, 2016

Manuel Jordan opened SPR-13868 and commented

Hello

I know that @RestController avoid use or declare @ResponseBody
Just curious if is possible apply for @RequestBody too

Perhaps it should be automatically applied to always for the first parameter of the method. Therefore leaving free to declare other parameters.

I mean from

@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value=PersonaRestControllerSupport.ROOT_URI,
                                method=RequestMethod.POST,
                                consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Void> saveOne(@Validated @RequestBody Persona persona){

to (No @RequestBody )

@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value=PersonaRestControllerSupport.ROOT_URI,
                                method=RequestMethod.POST,
                                consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Void> saveOne(@Validated Persona persona){

In case of many parameters in Rest (Perhaps get the URL, or Header) apply something similar about the following (about Parameter order constrains/restrictions)

public String saveOne(@Validated @ModelAttribute Persona persona,
                                    BindingResult result,
                                    RedirectAttributes redirectAttributes){

Where BindingResult must be located after of the Entity Model.

Thanks


Affects: 4.2 GA

Issue Links:

  • #18315 Controller method argument initialized either from the request body or from request parameters
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jan 20, 2016

Rossen Stoyanchev commented

hi Manuel Jordan, currently a controller method argument without annotations (and not handled by any other resolver) is treated in one of two ways. A "simple" parameter type such as String, Number, Date, URI, Locale, etc (as defined by BeanUtils.isSimpleProperty) is treated as a request parameter while any other Object is treated as a command object (i.e. a model attribute) whose properties are bound to request parameters. The @RequestBody annotation then switches those semantics to convert the request body.

I recognize that a lot of REST controllers commonly use the body of the request so flipping the semantics with @RestController does make some sense. For once I'm afraid it's too late to do that at this stage without causing a lot of disruption. Furthermore a @RestController may very well be picking up request parameters (either as a simple type or command object mapped to multiple request parameters) through arguments without annotations. See the discussion under #18315 which shows that a "rest" controller is quite likely to pick up query parameters depending on the Ajax component library you use.

If we did assume @RequestBody by default, we'd have a hard time dealing with "simple" types. It's possible that the intent is to use @RequestBody (since we do have StringHttpMessageConverter and also ObjectToStringHttpMessageConverter backed by a ConversionService) but still it's just as (if not more) likely that you actually want to map to query parameters. So although it does require a little extra effort and space to have @RequestBody I still find it a nice way to express the intent to deserialize the body of the request as opposed to a more natural starting point such as picking up query parameters which are legitimate input in REST interactions too.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jan 21, 2016

Manuel Jordan commented

Hi Rossen

Thanks a lot by the detailed explanation.

Seems very interesting the #18315
Now I am watching that post.

To be honest I like use @RequestBody and @ResponseBody, but since @RestController avoids declare one, I thought, why not apply the same approach for other annotation…

-Manuel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment