The difference between @ ResponseBody and @ RequestBody annotations

  • 2021-08-03 08:11:18
  • OfStack

The difference between @ ResponseBody and @ RequestBody annotations

1 Preface

Before we dwell on the @ ResponseBody and @ RequestBody annotations, let's take a look at the @ RequestMapping annotation under 1, @ RequestMapping is an annotation that handles request address mappings and can be used on classes or methods. Used on a class to indicate that all methods in the class that respond to requests have this address as the parent path; Used on a method to indicate that appending the address in the annotation on the method under the parent path of the class will access the method. For example.


/**
*  Used on classes, you can have no 
*/

@RequestMapping(value = "/controllerDemo")
public class ControllerDemo {
  //  Used on a method, you must have 
  @RequestMapping(value = "/methodDemo")
  public String methodDemo() {
    return "helloWorld";
  }
}

Its corresponding action is "action = controllerDemo/methodDemo". Therefore, visiting http://localhost: 8080/controllerDemo/methodDemo locally will return (jump) to the "helloWorld. jsp" page.

2 annotation details

In this section, the author details the differences between @ ResponseBody and @ RequestBody annotations:

The @ Responsebody annotation indicates that the return result of this method is directly written into the HTTP response body (ResponseBody), which is generally used when obtaining data asynchronously; After using @ RequestMapping, the return value is usually resolved as a jump path, and after adding @ Responsebody, the return result is not resolved as a jump path, but is directly written into the HTTP response body. For example, retrieving json data asynchronously with the @ Responsebody annotation returns json data directly. The @ RequestBody annotation inserts the HTTP request body into the method and writes the request body to an object using the appropriate HttpMessageConverter.

For example,


@RequestMapping(value = "person/login")
@ResponseBody
public Person login(@RequestBody Person person) {  //  In the request  datas  Write  Person  Object 
  return person;  //  Will not be resolved as a jump path, but will be written directly to the  HTTP  In the response body 
}

Page asynchronous request:


function login() {
  var datas = '{"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 : datas,
    success : function(data) {
      alert("id: " + data.id + "name: " + data.name + "status: "+ data.status);
    },
    error : function() {
      alert('Sorry, it is wrong!');
    }
  });
};

3 Extension

Next, a @ PathVariable annotation is introduced, which is used to get dynamic parameters in the request path (url). For example,


/**
* @RequestMapping(value = "/person/profile/{id}/{name}/{status}")  In  {id}/{name}/{status}
*  And  @PathVariable int id , @PathVariable String name , @PathVariable boolean status
* 11 Match by name. 
*/

@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);
}

Page asynchronous request:


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 + "name: " + data.name + "status: "
        + data.status);
  });
}

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: