Skip to content

Commit

Permalink
Add example of annotating method argument with @Valid @requestbody.
Browse files Browse the repository at this point in the history
Switch to using @ModelAttribute for application/x-www-form-urlencoded data (instead of MultiValueMap).
  • Loading branch information
rstoyanchev committed Aug 31, 2011
1 parent cba02da commit 18f744b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
@@ -1,13 +1,24 @@
package org.springframework.samples.mvc.messageconverters;

import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class JavaBean {

private String foo = "bar";
@NotNull
private String foo;

private String fruit = "apple";
@NotNull
private String fruit;

public JavaBean() {
}

public JavaBean(String foo, String fruit) {
this.foo = foo;
this.fruit = fruit;
}

public String getFoo() {
return foo;
Expand All @@ -24,5 +35,10 @@ public String getFruit() {
public void setFruit(String fruit) {
this.fruit = fruit;
}

@Override
public String toString() {
return "JavaBean {foo=[" + foo + "], fruit=[" + fruit + "]}";
}

}
@@ -1,8 +1,11 @@
package org.springframework.samples.mvc.messageconverters;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -27,11 +30,11 @@ public class MessageConvertersController {
return "Wrote a string";
}

// FormHttpMessageConverter (note: not recommended for reading browser form posts. Use standard JavaBean form binding instead. see 'form' showcase/package).
// Form encoded data (application/x-www-form-urlencoded)

@RequestMapping(value="/form", method=RequestMethod.POST)
public @ResponseBody String readForm(@RequestBody MultiValueMap<String, String> form) {
return "Read form map " + form;
public @ResponseBody String readForm(@ModelAttribute JavaBean bean) {
return "Read x-www-form-urlencoded: " + bean;
}

@RequestMapping(value="/form", method=RequestMethod.GET)
Expand All @@ -46,24 +49,24 @@ public class MessageConvertersController {

@RequestMapping(value="/xml", method=RequestMethod.POST)
public @ResponseBody String readXml(@RequestBody JavaBean bean) {
return "Read from XML " + bean;
return "Read from XML: " + bean;
}

@RequestMapping(value="/xml", method=RequestMethod.GET)
public @ResponseBody JavaBean writeXml() {
return new JavaBean();
return new JavaBean("bar", "fruit");
}

// MappingJacksonHttpMessageConverter (requires Jackson on the classpath - particularly useful for serving JavaScript clients that expect to work with JSON)

@RequestMapping(value="/json", method=RequestMethod.POST)
public @ResponseBody String readJson(@RequestBody JavaBean bean) {
return "Read from JSON " + bean;
public @ResponseBody String readJson(@Valid @RequestBody JavaBean bean) {
return "Read from JSON: " + bean;
}

@RequestMapping(value="/json", method=RequestMethod.GET)
public @ResponseBody JavaBean writeJson() {
return new JavaBean();
return new JavaBean("bar", "fruit");
}

// AtomFeedHttpMessageConverter (requires Rome on the classpath - useful for serving Atom feeds)
Expand Down
30 changes: 19 additions & 11 deletions src/main/webapp/WEB-INF/views/home.jsp
Expand Up @@ -180,11 +180,11 @@
<ul>
<li>
<form id="readForm" action="<c:url value="/messageconverters/form" />" method="post">
<input id="readFormSubmit" type="submit" value="Read a Form" />
<input id="readFormSubmit" type="submit" value="Read Form Data" />
</form>
</li>
<li>
<a id="writeForm" href="<c:url value="/messageconverters/form" />">Write a Form</a>
<a id="writeForm" href="<c:url value="/messageconverters/form" />">Write Form Data</a>
</li>
</ul>
<h3>Jaxb2RootElementHttpMessageConverter</h3>
Expand All @@ -205,6 +205,11 @@
<input id="readJsonSubmit" type="submit" value="Read JSON" />
</form>
</li>
<li>
<form id="readJsonInvalid" class="readJsonForm invalid" action="<c:url value="/messageconverters/json" />" method="post">
<input id="readInvalidJsonSubmit" type="submit" value="Read JSON (Validation Error)" />
</form>
</li>
<li>
<a id="writeJson" class="writeJsonLink" href="<c:url value="/messageconverters/json" />">Write JSON</a>
</li>
Expand Down Expand Up @@ -250,7 +255,7 @@
</ul>
<ul>
<li>
<a href="<c:url value="/views/pathVars/bar/apple" />">@PathVariables in the model when rendering</a>
<a href="<c:url value="/views/pathVars/bar/apple" />">Using path variables in a view template</a>
</li>
</ul>
<ul>
Expand Down Expand Up @@ -383,13 +388,13 @@
$(document).ready(function() {
$("#tabs").tabs();
$("a[class=textLink]").click(function(){
$("a.textLink").click(function(){
var link = $(this);
$.ajax({ url: link.attr("href"), dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
return false;
});
$("form[class=textForm]").submit(function(event) {
$("form.textForm").submit(function(event) {
var form = $(this);
var button = form.children(":first");
$.ajax({ type: "POST", url: form.attr("action"), data: "foo", contentType: "text/plain", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
Expand All @@ -409,14 +414,14 @@ $(document).ready(function() {
return false;
});
$("form[class=readXmlForm]").submit(function() {
$("form.readXmlForm").submit(function() {
var form = $(this);
var button = form.children(":first");
$.ajax({ type: "POST", url: form.attr("action"), data: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><javaBean><foo>bar</foo><fruit>apple</fruit></javaBean>", contentType: "application/xml", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
return false;
});
$("a[class=writeXmlLink]").click(function() {
$("a.writeXmlLink").click(function() {
var link = $(this);
$.ajax({ url: link.attr("href"),
beforeSend: function(req) {
Expand All @@ -432,14 +437,17 @@ $(document).ready(function() {
return false;
});
$("form[class=readJsonForm]").submit(function() {
$("form.readJsonForm").submit(function() {
var form = $(this);
var button = form.children(":first");
$.ajax({ type: "POST", url: form.attr("action"), data: "{ \"foo\": \"bar\", \"fruit\": \"apple\" }", contentType: "application/json", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
var button = form.children(":first");
var data = form.hasClass("invalid") ?
"{ \"foo\": \"bar\" }" :
"{ \"foo\": \"bar\", \"fruit\": \"apple\" }";
$.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;
});
$("a[class=writeJsonLink]").click(function() {
$("a.writeJsonLink").click(function() {
var link = $(this);
$.ajax({ url: this.href, dataType: "json", success: function(json) { MvcUtil.showSuccessResponse(JSON.stringify(json), link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
return false;
Expand Down

0 comments on commit 18f744b

Please sign in to comment.