SpringMVC(四)Message Converters

字符串信息转换

获取简单字符串
@RequestMapping(value="/string", method=RequestMethod.POST)
public @ResponseBody String readString(@RequestBody String string) {
    return "Read string '" + string + "'";
}

这里的字符串是前台通过ajax传入的,默认为Content-type 是text-plain,所以这里需要通过@RequestBody取参数,如果是通过form提交的参数或者通过url传的参数,则通过RequestParam 获取参数,前端提交代码如下:

    $("form.textForm").submit(function(event) {
        var form = $(this);
        var button = form.children(":first");
        $.ajax({ type: "POST", url: form.attr("action"), data: "foo", contentType: "text/plain", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
        return false;
    });
返回简单字符串
@RequestMapping(value="/string", method=RequestMethod.GET)
public @ResponseBody String writeString() {
    return "Wrote a string";
}

设置@ResponseBody 可以将方法返回值输出到response体中,进而前台可读取返回值

读写表单信息

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

表单提交的参数可以用@ModelAttribute获取,JavaBean中的属性名必须和表单的参数名一致。SpringMVC会自动获取参数并转换类型,设置到bean的属性上。这里的注解@ModelAttribute也可以省略,直接将JavaBean作为方法参数,能达到同样效果。@ModelAttribute用来获取一组参数,@RequestParam用来获取单个参数。

返回表单信息

前台指定Accept:application/x-www-form-urlencoded 后,返回的response Content-type也必须是 application/x-www-form-urlencoded.FormHttpMessageConverter 就是处理表单返回信息的。Controller 方法返回结果是MultiValueMap会自动调用FormHttpMessageConverter写入response信息。如下段代码所示:

@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;
}

对应的请求代码是

    $("#writeForm").click(function() {
        var link = $(this);
        $.ajax({ url: this.href, dataType: "text", beforeSend: function(req) { req.setRequestHeader("Accept", "application/x-www-form-urlencoded"); }, success: function(form) { MvcUtil.showSuccessResponse(form, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});                 
        return false;
    });

返回MultiValueMap为什么会调用FormHttpMessageConverterk呢?通过查看FormHttpMessageConverterk源码就知道其中的原理:

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        if (!MultiValueMap.class.isAssignableFrom(clazz)) {
            return false;
        }
        if (mediaType == null || MediaType.ALL.equals(mediaType)) {
            return true;
        }
        for (MediaType supportedMediaType : getSupportedMediaTypes()) {
            if (supportedMediaType.isCompatibleWith(mediaType)) {
                return true;
            }
        }
        return false;
    }

读写xml信息

获取前台传入的xml内容

前端代码如下所示,通过ajax发送请求,并传入xml数据:

    $("form.readXmlForm").submit(function() {
        var form = $(this);
        var button = form.children(":first");
        $.ajax({ type: "POST", url: form.attr("action"), data: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><javaBean><foo>bar</foo><fruit>apple</fruit></javaBean>", contentType: "application/xml", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
        return false;
    });

Controller代码如下所示,SpringMvc会根据Content-type自动选择Jaxb2RootElementHttpMessageConverter,调用Jaxb2RootElementHttpMessageConverter的readFromSource方法,即可将xml字符串换成JavaBean。

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

前端请求代码如下所示,在发起请求时设置Accept:application/xml

    $("a.writeXmlLink").click(function() {
        var link = $(this);
        $.ajax({ url: link.attr("href"),
            beforeSend: function(req) { 
                if (!this.url.match(/\.xml$/)) {
                    req.setRequestHeader("Accept", "application/xml");
                }
            },
            success: function(xml) {
                MvcUtil.showSuccessResponse(MvcUtil.xmlencode(xml), link);
            },
            error: function(xhr) { 
                MvcUtil.showErrorResponse(xhr.responseText, link);
            }
        });
        return false;
    }); 

Controller代码如下所示,SpringMVC会根据Accept:application/xml自动选择Jaxb2RootElementHttpMessageConverter处理Controller返回值,将返回的JavaBean或Map转换成xml字符串写入response中。

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

读写JSON信息

读取JSON信息

前端代码如下所示,发起ajax请求时设置 contentType:application/json

    $("form.readJsonForm").submit(function() {
        var form = $(this);
        var button = form.children(":first");
        var data = form.hasClass("invalid") ?
                "{ \"foo\": \"bar\" }" : 
                "{ \"foo\": \"bar\", \"fruit\": \"apple\" }";
        $.ajax({ type: "POST", url: form.attr("action"), data: data, contentType: "application/json", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
        return false;
    });

SpringMVC会根据contentType:application/json选择MappingJacksonHttpMessageConverter将请求体中的内容转换成JavaBean或Map。Controller代码如下所示:

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

前端代码如下所示,发起ajax请求时设置Accept:application/json:

    $("a.writeJsonLink").click(function() {
        var link = $(this);
        $.ajax({ url: this.href,
            beforeSend: function(req) {
                if (!this.url.match(/\.json$/)) {
                    req.setRequestHeader("Accept", "application/json");
                }
            },
            success: function(json) {
                MvcUtil.showSuccessResponse(JSON.stringify(json), link);
            },
            error: function(xhr) {
                MvcUtil.showErrorResponse(xhr.responseText, link);
            }});
        return false;
    });

Controller代码如下所示,SpringMVC根据前端发起请求时设置的Accept:application/json自动选择MappingJackson2HttpMessageConverter,通过该Converter将JavaBean转换成json,并写入到response中去。另外前端发请求时如果不设置Accept,将请求url加上.json后缀,SpringMVC也会选择该Converter处理信息。

@RequestMapping(value="/json", method=RequestMethod.GET)
public @ResponseBody JavaBean writeJson() {
    return new JavaBean("bar", "apple");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,183评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,596评论 18 399
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,569评论 25 709
  • 做人,不要瞧不起别人 人活着,谁都不用瞧不起谁。 三年河东,三年河西, 潮起潮又落。 家财万贯,买不了太阳不下山;...
    正能量心语阅读 3,490评论 0 1
  • 8月8日夜间9时19分后,我们得知九寨沟地区发生了7.0级地震。 同事们在下大雨的北京加班,紧急修改上线了地震寻人...
    Jason月月阅读 3,434评论 0 0

友情链接更多精彩内容