java - @RequestBody and @ResponseBody annotations in Spring -


can explain @requestbody , @responsebody annotations in spring 3? for? examples great.

there whole section in docs called 16.3.3.4 mapping request body @requestbody annotation. , 1 called 16.3.3.5 mapping response body @responsebody annotation. suggest consult sections. relevant: @requestbody javadocs, @responsebody javadocs

usage examples this:

using javascript-library jquery, post json-object this:

{ "firstname" : "elmer", "lastname" : "fudd" } 

your controller method this:

// controller @responsebody @requestmapping("/description") public description getdescription(@requestbody userstats stats){     return new description(stats.getfirstname() + " " + stats.getlastname() + " hates wacky wabbits"); }  // domain / value objects public class userstats{     private string firstname;     private string lastname;     // + getters, setters } public class description{     private string description;     // + getters, setters, constructor } 

now if have jackson on classpath (and have <mvc:annotation-driven> setup), spring convert incoming json userstats object post body (because added @requestbody annotation) , serialize returned object json (because added @responsebody annotation). browser / client see json result:

{ "description" : "elmer fudd hates wacky wabbits" } 

see previous answer of mine complete working example: https://stackoverflow.com/a/5908632/342852

note: requestbody / responsebody of course not limited json, both can handle multiple formats, including plain text , xml, json used format.


update: ever since spring 4.x, won't use @responsebody on method level, rather @restcontroller on class level, same effect. see creating rest controllers @restcontroller annotation


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -