@ Responsebody and @ RequestBody, @ responsebody

Source: Internet
Author: User

@ Responsebody and @ RequestBody, @ responsebody

Prerequisites: @ RequestMappingRequestMapping is an annotation for Processing request address ing and can be used for classes or methods. Used on the class to indicate that all methods in the class that respond to requests use this address as the parent path. @ RequestMapping (value = "/aaa") // class level. public class myController {@ RequestMapping (value = "/bbb") may not exist. // method level, public String getMyName () {return "myReturn" ;}} must correspond to the following actions: <form action = "aaa/bbb"> the return page is myReturn. jsp @ responsebody and @ RequestBody @ responsebody indicate that the returned results of this method are directly written into the HTTP response body, which is generally used for asynchronous data retrieval. After @ RequestMapping is used, the return value is usually parsed as a jump path. After @ responsebody is added, the returned result is not parsed as a jump path, but directly written into the HTTP response body. For example, json data is obtained asynchronously. After @ responsebody is added, json data is directly returned. @ RequestBody: insert the HTTP Request body into the method, and use the appropriate HttpMessageConverter to write the Request body to an object. Function login () {// asynchronous page 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 + "\ nstat Us: "+ data. status) ;}, error: function () {alert ('error! ') ;}}) ;};@ RequestMapping (value = "person/login") @ ResponseBody public Person login (@ RequestBody Person person) {// write the mydata in the request to return Person in the person object; // it will not be parsed as a jump path, but will be directly written into the HTTP response body} extension: @ PathVariable get the request PATH variable function profile () {var url = "$ {pageContext. request. contextPath}/person/profile/"; var query = $ ('# id '). val () + '/' + $ ('# name '). val () + '/' + $ ('# status '). val (); url + = query; $. ge T (url, function (data) {alert ("id:" + data. id + "\ nname:" + data. name + "\ nstatus:" + data. status) ;}) ;}@ RequestMapping (value = "person/profile/{id}/{name}/{status }") @ ResponseBody public Person porfile (@ PathVariable int id, @ PathVariable String name, @ PathVariable boolean status) {return new Person (id, name, status );} // @ RequestMapping (value = "/person/profile/{id}/{name}/{status}") {id}/{na Me}/{status} and @ PathVariable int id, @ PathVariable String name, @ PathVariable boolean status match by name.

 

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.