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








  • 相关阅读:
    向量旋转公式推导
    atan函数与atan2函数
    UVALive 7040 Color (容斥原理+逆元+组合数+费马小定理+快速幂)
    关于source insight、添加.s和.S文件,显示全部路径、加入项目后闪屏幕
    linux内核设计与实现--进程调度 系统调用
    linux内核设计与实现--进程管理
    linux命令行与shell脚本编程大全---bash shell命令
    linux内核设计与实现--从内核出发
    Linux内核学习之路
    NAND FLASH均衡算法笔记(转)
  • 原文地址:https://www.cnblogs.com/lavieenrose/p/2418768.html
Copyright © 2011-2022 走看看