I have the following situation, in my view I have the following form:
<form id="readJson" class="readJsonForm" action="<c:url value="/messageconverters/json" />" method="post">
<input id="readJsonSubmit" type="submit" value="Read JSON" />
</form>
Related to the submit event of this form form (having class="readJsonForm) I have the following Jquery callback function:
$("form.readJsonForm").submit(function() {
// Riferimento all'elemento che ha scatenato l'evento submit (il form)
var form = $(this);
var button = form.children(":first");// Seleziona il bottone submit
// OPERATORE CONDIZIONALE: il form ha classe "invalid" ?
var data = form.hasClass("invalid") ?
"{ \"foo\": \"bar\" }" : // SI: foo = bar
// NO: foo= bar ; fruit = apple
"{ \"foo\": \"bar\", \"fruit\": \"apple\" }";
/* AJAX CALL PARAMETER:
type: Say to the servlet tath 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) { // CASO DI SUCCESSO:
/* Passa al metodo il testo ritornato dalla chiamata AJAX ed il
riferimento nel DOM al bottone accanto ala quale mostrare
tale output */
MvcUtil.showSuccessResponse(text, button);
},
error: function(xhr) { // CASO DI ERRORE
MvcUtil.showErrorResponse(xhr.responseText, button);
}
});
return false;
});
So this Jquery function create a new JSON object having the two properties (foo and fruit) valorized in this way:
foo = bar
fruit = apple
The HTTP request is handled by the following method of my controller class:
/* Metodo che gestisce HTTP Request di tipo POST dirette verso
* l'URL: "/messageconverters/json"
* @param L'oggetto JSON inserito all'interno del campo body dell'HTTP
* su cui viene eseguita una validazione
*
*/
@RequestMapping(value="/json", method=RequestMethod.POST)
public @ResponseBody String readJson(@Valid @RequestBody JavaBean bean) {
return "Read from JSON: " + bean;
}
this method simply take the JSON object from the body field of the HTTP Request and, using Jaxb2RootElementHttpMessageConverter transport it in a new JavaBean object
In my case JavaBean object is an object having only two properties: foo and fruit and getter, setter and toString() methods, something like this:
@XmlRootElement public class JavaBean {
@NotNull
private String foo;
@NotNull
private String fruit;
public JavaBean() {
}
public JavaBean(String foo, String fruit) {
this.foo = foo;
this.fruit = fruit;
}
// GETTER, SETTER & toString() methods
Ok, so the value inside the JSON object are put inside the JavaBean object variables having the same name...this thing is pretty clear for me.
I have some problem about the rule of @Valid annotation.
My JavaBean parameter is annoted using the @Valid annotation, reading the documentation I have understand that this is not a Spring annotation but this is related to the Validation Framework JSR-303 Validation API
I know only a litle bit about this API and I remember that @Valid triggered the validation on the object fields
But I remember that my object fields (my variables inside JavaBeans object) have to be annothed using some validation annotation, for example like @NotNull or using a personal validation Java class that implement my personal validator.
In this case I have nothing about it, I only have the @Valid annotation on my method parameter...
What exactly do in this case?
The only thing that I can think is that check if my JSON object is correctly mapped with the JavaBean object (if have the same valorized properties), and for example...if the JSON object have only one properties valorized go into error...
Someone can help me?
Tnx Andrea