@ResponseBody notes the role and principles

1, the concept of

        Annotations @ResponseBody, the method used in the control layer (Controller) a.

2, the role of

        Action: The method returns the value written to the response of the body region in a specific format, and then returns the data to the client.

        When the above method is not written ResponseBody, the return value will be the underlying methods for the package ModelAndView object.

        If the return value is a string, then the string to the client directly; if an object, the object will be converted to json string, and then written to the client.

3, pay attention to coding

        If the object returns, according utf-8 encoded. If the return String, by default iso8859-1 encoding, the page may be garbled. Therefore, we can manually modify the annotations encoding format, e.g. @RequestMapping (value = "/ cat / query", produces = "text / html; charset = utf-8"), a route request in front of, behind encoding format.

4, principle

        Layer control how the return value is converted into a string json format? HttpMessageConverter actually achieved by the method, which is an interface present, to complete the conversion on its implementation class. If the bean is an object, the object will call getXXX () method of getting a property value and encapsulated in the form of key-value pairs, then into json string. If the map is set, using the get (key) value value acquisition mode, and then encapsulated.

Guess you like

Origin blog.csdn.net/jiahao1186/article/details/91980316