In order to demonstrate this, I first set up a page that’ll POST a request to a controller.
<form:form method="POST" action="requestbody"> <input type="submit" name="action" id="request_body_test" value="<spring:message code="label.request.body"/>" /> <input type="hidden" id="age" name="age" value="23" /> <input type="hidden" id="gender" name="gender" value="male" /> </form:form>
In the above HTML, the value of 'label.request.body' = 'Test the @RequestBody Annotation, map to a String'. Also note that I’ve used a Http POST above as GET is not supported.
The next step is to create a controller handler method, annotating one of the arguments appropriately. In this simple example, I’m getting hold of the request body and echoing it back to the browser.
@Controller
public class MiscAnnotationsController {
@RequestMapping(value = "/requestbody", method = RequestMethod.POST)
public void writeRequestBody(@RequestBody String body, Writer writer) throws IOException {
// This will be the contents of the next page you see
writer.write(body);
}
}
action=Test+the+%40RequestBody+Annotation%2C+map+to+a+String&age=23&gender=male
No comments:
Post a comment