Spring MVC Getting Started Guide (i): @RequestBody, Introduction to @ResponseBody annotations

Source: Internet
Author: User
Tags bind html tags json
This article reprinted address: http://blog.csdn.net/walkerjong/article/details/7520896 Introduction: @RequestBody

Role:

i) This annotation is used to read the body portion of the request requests, parse using the httpmessageconverter of the system default configuration, and then bind the corresponding data to the object to be returned;

II) Then bind the object data returned by Httpmessageconverter to the parameters of the method in the controller.

Use time:

A) GET, post method, according to request header Content-type value to judge: application/x-www-form-urlencoded, Optional (that is, not required, Because the data of this kind of circumstance @requestparam, @ModelAttribute can also deal with, of course @requestbody also can handle); Multipart/form-data, Cannot process (i.e. use @requestbody cannot process data in this format); Other formats must be (other formats include Application/json, Application/xml, etc.). Data in these formats must be handled using @requestbody);

B) When put is submitted, it is judged according to the value of the request header Content-type:

Application/x-www-form-urlencoded, must; multipart/form-data, cannot handle; other formats, must;
Description: The data encoding format of the body part of request is specified by the Content-type of the header section;


@ResponseBody

Role:

The annotation is used to write the object returned by the controller's method to the body data area of the response object after the appropriate httpmessageconverter is converted to the specified format.

Use time:

The returned data is not a page of HTML tags, but is used in some other form of data (JSON, XML, etc.);



Httpmessageconverter

[java]  View plain  copy <span style= "font-family: ' Microsoft yahei ';" >/**   * strategy interface that specifies a converter that  can convert from and to HTTP requests and responses.    *   *  @author  Arjen Poutsma   *  @author  Juergen Hoeller   *  @since  3.0   */   public interface httpmessageconverter<t>  {          /**       * indicates  whether the given class can be read by this converter.        *  @param  clazz the class to test for  readability       *  @param  mediaType the media  Type to read, can be {@code  null} if not specified.       *  typically the value of a {@code  Content-Type} header.        *  @return  {@code  true} if readable; {@code  false}  otherwise       */       boolean canread (Class <?> clazz, mediatype mediatype);          /**       * indicates whether the given class can be  written by this converter.       *  @param  clazz  the class to test for writability       * @ param mediatype the media type to write, can be {@code  null}  if not specified.       * typically the value of an { @code  Accept} header.       *  @return  {@code  true}  if writable; {@code  false} otherwise       */       boolean canwrite (Class<?> clazz, mediatype mediatype);           /**       * Return the  list of {@link  MediaType} objects supported by this converter.       *  @return  the list of supported media types        */       List<MediaType>  Getsupportedmediatypes ();          /**        * read&nBsp;an object of the given type form the given input message , and returns it.       *  @param  clazz the  type of object to return. this type must have previously  been passed to the       * {@link   #canRead  canread}  method of this interface, which must have returned {@code   true}.       *  @param  inputMessage the HTTP input  message to read from       *  @return  the converted  object       *  @throws  ioexception in case of  I/O errors       *  @throws  httpmessagenotreadableexception  in case of conversion errors       */       t  read (class<? extends t> clazz, httpinputmessage inputmessage)                throws

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.