it技术IT技术征服Spring

SpringMVC(四)Message Converters

2016-11-23  本文已影响335人  yongguang423

字符串信息转换

获取简单字符串
@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");
}
上一篇下一篇

猜你喜欢

热点阅读