Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring @RequestBody support the GET method?

Tags:

I am trying to carry JSON data in an HTTP GET request message, but my Spring MVC server can't seem to retrieve the JSON data from the GET request body.

like image 823
Hongbo Tian Avatar asked Jan 22 '16 22:01

Hongbo Tian


People also ask

Can we use @RequestBody in Get method?

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request.

Can we send body in GET request in Spring boot?

In this article, we will discuss how to get the body of the incoming request in the spring boot. @RequestBody: Annotation is used to get request body in the incoming request. Note: First we need to establish the spring application in our project. Step 2: Click on Generate which will download the starter project.

What does @RequestBody do in Spring?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.

What is get method in Spring boot?

Write and Test a GET Method This method would just return a string “Welcome to Spring Boot” when the someone accesses /welcome. To do this, create a new class and name it welcomeController. If you have done everything right, the content of you welcomeController file would be as shown below.


1 Answers

HTTP's GET method does not include a request body as part of the spec. Spring MVC respects the HTTP specs. Specifically, servers are allowed to discard the body. The request URI should contain everything needed to formulate the response.

If you need a request body, change the request type to POST, which does include the request body.

like image 141
Leland Barton Avatar answered Sep 22 '22 18:09

Leland Barton