Reputation: 42957
I am studying the Spring MVS Showcase downloadable from di STS dashboard. I am studying how Spring maps the request and I am having some problem to understand the following thing:
I have this form with a submit button:
<li>
<form id="byConsumes" class="readJsonForm" action="<c:url value="/mapping/consumes" />" method="post">
<input id="byConsumesSubmit" type="submit" value="By consumes" />
</form>
</li>
When I click on the submit button a Jquery function that creates a JSON object is passed by the HTTP Post Request, this is the code of the JQuery function:
$("form.readJsonForm").submit(function() {
var form = $(this); // Variabile che si riferisce all'elemento nel DOM che ha scatenato l'evento click (il form)
var button = form.children(":first"); // Seleziona il bottone submit
var data = form.hasClass("invalid") ? // OPERATORE CONDIZIONALE: il form ha classe "invalid" ?
"{ \"foo\": \"bar\" }" : // SI: foo = bar
"{ \"foo\": \"bar\", \"fruit\": \"apple\" }"; // NO: foo= bar ; fruit = apple
/* AJAX CALL PARAMETER:
type: Say to the servlet path, the request is a POST HTTP Request
url: The address to which to send the call
data: the content of my data variable
contentType: an object having JSON format
dataType: the type of content returned by the server
*/
$.ajax({ type: "POST", url: form.attr("action"), data: data, contentType: "application/json", dataType: "text",
success: function(text) { MvcUtil.showSuccessResponse(text, button); },
error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
return false;
});
The JSON object that I have create and passed is represented by the data variable and contain the following key\value: { \"foo\": \"bar\", \"fruit\": \"apple\" }
Something like:
foo: bar
fruit: apple
Now, in my controller I have the method that handles this request:
@RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) {
return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')";
}
So it is clear for me that this method handles HTTP Post Request towards "mapping/consumes" path (only POST Request) but I am not sure about the meaning of the following items:
consumes=MediaType.APPLICATION_JSON_VALUE : what exactly does this mean? I think that it says to Spring that this method receives an object in JSON format, so it can be parsed in some way...but I am not sure about it and I have not found it in the documentation.
What is consumes? a variable or something like an annotation? I am not understanding things because here it is a parameter of the @RequestMapping annotation but searching on Google shows it is used as a standalone annotation...
In my byConsumes() method I have the following input parameter: @RequestBody JavaBean javaBean. Reading the Spring documentation I understand that: @RequestBody method parameter annotation indicates that using @RequestBody annotation a method parameter should be bound to the value of the HTTP request body.
So in practice this thing mean that I have my JSON object inside my HTTP Request body field and using this annotation I am converting it in an object named javaBean having class JavaBean?
If my affirmation is true...what kind of object is a JavaBean type object? an object that contains only a number of variables and the corresponding getter and setter methods? (in the previus case an object that contain only two variable: the first one named foo and having value "bar", the second one having name fruit and having value "apple")
is it right?
Thank you very much Andrea
Upvotes: 3
Views: 15528
Reputation: 799
Your interpretation of what this code is doing is essentially correct.
'Consumes' is a parameter of the annotation @RequestMapping
which is used to indicate the HTTP content type that the method handles. In this case it is indicating to Spring that the method takes JSON as input. You can read more about this in the Spring documentation here. You could similarly indicate that a method 'consumes' XML.
As for @RequestBody
annotation, this is used to indicate the annotated method parameter should be composed from the body of the HTTP request, the documentation for it is here. Spring will usually do this by taking the name of the HTTP parameter in the request and setting the value in the Java bean using a setter method.
In your example, Spring will inspect the annotations of the method and determine that the request body should be mapped to an object of type JavaBean
. It will create an instance of that class automatically, and (via a Message Converter) will populate the JavaBean
instance. Spring will use reflection to do all of this, but the code it executes would essentially do this:
JavaBean parameter = new JavaBean();
parameter.setFoo("bar");
parameter.setFruit("apple");
You could swap out the JavaBean
type for any other Java class that declares getters/setters for fields foo
and fruit
.
Upvotes: 8