[springmvc]HttpMessageConverters

  • Behind the scenes, a HttpMessageConverter underpins reading the request body and generating the response
  • Multiple converters may be registered for different content types
  • For @RequestBody, the first converter that can read the POSTed "Content-Type" into the desired method parameter type is used
  • For @ResponseBody, the first converter that can write the method return type into one of the client’s “Accept”ed content types is used
  • Default set of HttpMessageConverters registered for you

1. StringHttpMessageConverter

    @RequestMapping(value="/string", method=RequestMethod.POST)
public @ResponseBody String readString(@RequestBody String string) {
return "Read string '" + string + "'";
}

@RequestMapping(value="/string", method=RequestMethod.GET)
public @ResponseBody String writeString() {
return "Wrote a string";
}

2. FormHttpMessageConverter

  @RequestMapping(value="/form", method=RequestMethod.POST)
public @ResponseBody String readForm(@ModelAttribute JavaBean bean) {
return "Read x-www-form-urlencoded: " + bean;
}

@RequestMapping(value="/form", method=RequestMethod.GET)
public @ResponseBody MultiValueMap<String, String> writeForm() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("foo", "bar");
map.add("fruit", "apple");
return map;
}

3. Jaxb2RootElementHttpMessageConverter

    @RequestMapping(value="/xml", method=RequestMethod.POST)
public @ResponseBody String readXml(@RequestBody JavaBean bean) {
return "Read from XML: " + bean;
}

@RequestMapping(value="/xml", method=RequestMethod.GET)
public @ResponseBody JavaBean writeXml() {
return new JavaBean("bar", "fruit");
}

4. MappingJacksonHttpMessageConverter

    @RequestMapping(value="/json", method=RequestMethod.POST)
public @ResponseBody String readJson(@Valid @RequestBody JavaBean bean) {
return "Read from JSON: " + bean;
}

@RequestMapping(value="/json", method=RequestMethod.GET)
public @ResponseBody JavaBean writeJson() {
return new JavaBean("bar", "fruit");
}

client:
$(function(){   $("#json").click(function(){     var data = "{ \"foo\": \"bar1\", \"fruit\": \"apple\" }";     $.ajax({       type: "POST",
      url: $("#jsonaction").val(),       data: data,       contentType: "application/json",
      dataType: "json",       success: function(text) {         alert(text.foo);
      },       error: function(xhr) {         alert(xhr.responseText);       }
    });   }); });

5. AtomFeedHttpMessageConverter

    @RequestMapping(value="/atom", method=RequestMethod.POST)
public @ResponseBody String readFeed(@RequestBody Feed feed) {
return "Read " + feed.getTitle();
}

@RequestMapping(value="/atom", method=RequestMethod.GET)
public @ResponseBody Feed writeFeed() {
Feed feed = new Feed();
feed.setFeedType("atom_1.0");
feed.setTitle("My Atom feed");
return feed;
}

6. RssChannelHttpMessageConverter

    @RequestMapping(value="/rss", method=RequestMethod.POST)
public @ResponseBody String readChannel(@RequestBody Channel channel) {
return "Read " + channel.getTitle();
}

@RequestMapping(value="/rss", method=RequestMethod.GET)
public @ResponseBody Channel writeChannel() {
Channel channel = new Channel();
channel.setFeedType("rss_2.0");
channel.setTitle("My RSS feed");
channel.setDescription("Description");
channel.setLink("http://localhost:8080/mvc-showcase/rss");
return channel;
}

7. ByteArrayMessageConverter

  Reads “*/*” into a byte[]; writes Objects as “application/octet-stream”

8. SourceHttpMessageConverter

  Reads “text/xml” or “application/xml” into javax.xml.transform.Source

  Writes javax.xml.transform.Source to “text/xml” or “application/xml”

9. ResourceHttpMessageConverter

  Reads/writes org.springframework.core.io.Resource objects

10. BufferedImageHttpMessageConverter

  Reads/writes mime-types supported by Java Image I/O into BufferedImage

11. MarshallingHttpMessageConverter

  Reads/writes XML but allows for pluggability in Marshalling technology

12. Register your own or customize existing ones by setting the “messageConverters” property of the AnnotationMethodHandlerAdapter bean

  Easy to override the defaults using a BeanPostProcessor








原文地址:https://www.cnblogs.com/lavieenrose/p/2418768.html