zoukankan      html  css  js  c++  java
  • [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








  • 相关阅读:
    [置顶] Django 微信开发(一)——环境搭建
    opencv学习_5 (IplImage的结构)
    HDU 3910 (13.10.31)
    Python源码学习七 .py文件的解释
    Android高效加载大图、多图解决方案,有效避免程序内存溢出现象
    记录cocos2d-html5与cocosd-x jsb中遇到的坑
    【PAT Advanced Level】1011. World Cup Betting (20)
    Linux文件实时同步,可实现一对多
    mahout源码分析之DistributedLanczosSolver(五)Job over
    php引入lucene方法
  • 原文地址:https://www.cnblogs.com/lavieenrose/p/2418768.html
Copyright © 2011-2022 走看看