@ModelAttribute vs @RequestBody, @ResponseBody

I have referred many web-sites however unable to understand the following

1. What are the differences between @ModelAttribute and @ResponseBody
2. When to use @ModelAttribute and When to use @ResponseBody. Need examples to differentiate between the two.
3. Difference between @RequestBody and @ResponseBody. Need examples to differentiate between the two.
4. Difference between @ModelAttribute and @RequestBody

Thanks.
Software ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ambienceCommented:
A simple difference between @ModelAttribute and @RequestBody is that the former would look at the QueryString and map it to object fields whereas the latter would look at the Body of the HTTP request.

http://.../?name=Something&age=100
public String doSomething(@ModelAttribute User user) { }

Open in new window


{ "name": "Something", "age": "100" } in request body
public String doSomething(@RequestBodyUser user) { }

Open in new window


ResponseBody is simply annotating the returned value as the actual content of the HTTP response. It would be converted into appropriate format like JSon/XML. @ResponseBody will tell the framework to inhibit creation of a View.
0
Software ProgrammerAuthor Commented:
Now i understand the difference.

1. Now which one is the best approach ???
2. If both are for same purpose to bind to the bean..which one is the best practice or widely used as standard practice?
3. Does both handles multi-part file and equivalent options with one another ?
4. Does any one of them has lesser capabilities then the other one?
5. Which one is more secured in terms of security ?
0
ambienceCommented:
1. I would it boils down to the use of GET vs. POST methods and which one is better in the given situation. GET has some length limitations. Also see 4.
2. I suppose one could use both on a single method but not sure.
3. Dont think so.
4. RequestBody would be able to map complex JSON to say objects nested within objects. ModelAttribute I dont think it can do that.
5. Request body can be secured over HTTPS but Query parameters will always be visible.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Software ProgrammerAuthor Commented:
#3 - Seems this link https://javabeat.net/spring-multipart-file-upload/  says ModelAttribute supports multi-part file. this link https://stackoverflow.com/questions/14615692/how-do-i-upload-stream-large-images-using-spring-3-2-spring-mvc-in-a-restful-way says @RequestBody supports multi-part file..

Not sure why you don't think so. Also not sure about the above limitations of using one another

#4 - Does Query parameters will be always visible even when we have the http method as POST ??

#1 - what is the length limitations for GET vs POST ?
0
girionisCommented:
#1 - what is the length limitations for GET vs POST ?

This is implementation specific (each server/browser has its own limit).

#4 - Does Query parameters will be always visible even when we have the http method as POST ??

No. When you send query parameters as POST they are not part of the URL (and therefore they are not visible).
0
Software ProgrammerAuthor Commented:
@ModelAttribute and @RequestBody which one uses GET and which one uses POST....Does @ModelAttribute supports both GET and POST ???? Does @RequestBody supports both GET and POST ??
0
ambienceCommented:
A simple difference between @ModelAttribute and @RequestBody is that the former would look at the QueryString and map it to object fields whereas the latter would look at the Body of the HTTP request.

ModelAttribute -> GET
RequestBody -> POST

As the name implies "RequestBody" supports only Body/POST and ModelAttribute supports only GET
0
Software ProgrammerAuthor Commented:
so can we use RequestBody always since it takes care of everything...any drawback in using RequestBody always instead of ModelAttribute..Does RequestBody misses anything or any dis-advantage than ModelAttribute ?
0
ambienceCommented:
Have you read old comments?
0
Software ProgrammerAuthor Commented:
Yes i do. i understand RequestBody will take care of everything of ModelAttribute + Complex Json. I understand requestbody is superior than ModelAttribute. Please correct if i am wrong and let me know if RequestBody lacks things which ModelAttribute doesn't have.
0
ambienceCommented:
A correction:

"Request body can be secured over HTTPS but Query parameters will always be visible."

is incorrect. Both are safe from eavesdropping when used over HTTPS, i.e. sniffing won't be possible over SSL.
0
Software ProgrammerAuthor Commented:
So it means RequestBody can be used instead of ModelAttribute always..Is that right? Because we have all functionality of ModelAttribute in RequestBody..Is that correct?
0
girionisCommented:
So it means RequestBody can be used instead of ModelAttribute always..

No. RequestBody will not work with a GET.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Software ProgrammerAuthor Commented:
Thanks a lot for your patience and kind answers
0
girionisCommented:
Thank you for accepting. But to be fair I think you should also reward points to user "ambience".
0
Software ProgrammerAuthor Commented:
I am not sure about the point system. help me with the link to read.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.