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