@Responsebody and @requestbody

Source: Internet
Author: User

Preliminary knowledge: @RequestMappingRequestMapping is an annotation that handles request address mapping and can be used on classes or methods. On a class, the method that represents all response requests in a class is the parent path of the address. @RequestMapping (value = "/AAA")//Class level, you can have noPublicClassMycontroller {@RequestMapping (value = "/bbb")//Method level, you must havePublicString Getmyname () {Return "Myreturn"; }} The corresponding action is: <form action= "AAA/BBB" >The return page is myreturn.jsp@responsebody and @requestbody@responsebody means that the return result of the method is written directly to the HTTP response body, which is generally used when fetching data asynchronously. After using @requestmapping, the return value usually resolves to the jump path, plus @responsebody returns the result will not be resolved to the jump path, but is written directly to the HTTP response body. For example, asynchronously fetching JSON data, plus @responsebody, will return the JSON data directly. @RequestBody inserts the HTTP request body into the method, using the appropriate httpmessageconverter to write the request body to an object. function Login () {//Page asynchronous request var MyData = ' {' name ': ' ' + $ (' #name '). Val () + ' "," id ":" ' + $ (' #id '). Val () + ' "," status ":" ' + $ (' #status '). Val () + ' "}‘; $.ajax ({type: ' POST '), ContentType: ' Application/json ', url: "${pagecontext.request.contextpath}/person/login", ProcessData:False, DataType: ' JSON ', Data:mydata, success:function (data) {alert ("ID:" + data.id + "\nname:" + data.name + "\nstatus:" +Data.status); }, Error:function () {alert (' ERROR! ‘); } });}; @RequestMapping (value = "Person/login") @ResponseBodyPublic person Login (@RequestBody person person) {//Writes the MyData in the request to the Person objectreturn person;//is not resolved to a jump path, but is written directly to the HTTP response bodyExtension: @PathVariable Get Request Path variable function profile () {var url = "${pagecontext.request.contextpath}/person/profile/"; var query = $ (' #id '). Val () + '/' + $ (' #name '). Val () + '/' + $ (' #status ')). Val (); URL + = query; $.get (URL, function (data) {alert ("ID:" + data.id + "\nname:" + data.name + "\nsta Tus: "+ Data.status);}); @RequestMapping (value = "Person/profile/{id}/{name}/{status}" ) @ResponseBody public person Porfile (@PathVariable int ID, @PathVariable String name, @PathVariable Span style= "color: #0000ff;" >boolean status) {return new// @RequestMapping (value = "/person/profile/{id}/{name}/{status}") in {id}/{name}/{ Status} with the @pathvariable int ID, @PathVariable String name, @PathVariable boolean status one by one, matching by name. 

@Responsebody and @requestbody

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.