SPRING MVC3.2案例讲解--使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务(转)...
http://www.oschina.net/question/129540_25218
HttpMessageConverter( org.springframework.http.converter.HttpMessageConverter接口 ) 示例 :
StringHttpMessageConverter 从请求和响应读取/编写字符串。默认情况下,它支持媒体类型 text/* 并使用文本/无格式内容类型编写。
FormHttpMessageConverter 从请求和响应读取/编写表单数据。默认情况下,它读取媒体类型 application/x-www-form-urlencoded 并将数据写入 MultiValueMap<String,String>。
MarshallingHttpMessageConverter 使用 Spring 的 marshaller/un-marshaller 读取/编写 XML 数据。它转换媒体类型为 application/xml 的数据。
MappingJacksonHttpMessageConverter 使用 Jackson 的 ObjectMapper 读取/编写 JSON 数据。它转换媒体类型为 application/json 的数据。
AtomFeedHttpMessageConverter 使用 ROME 的 Feed API 读取/编写 ATOM 源。它转换媒体类型为 application/atom+xml 的数据。
RssChannelHttpMessageConverter 使用 ROME 的 feed API 读取/编写 RSS 源。它转换媒体类型为 application/rss+xml 的数据。
SourceHttpMessageConverter 处理请求类型 Request type: text/xml, application/xml
从HTTP request和response读写javax.xml.transform.Source .支持 DOMSource, SAXSource,StreamSource .
BufferedImageHttpMessageConverter 处理请求类型 Request type:支持Java I/O API,
从HTTP request和response中读写java.awt.image.BufferedImage
以上是IBM开源资源库中的一篇文章,我在这篇文章的基础上,结合springmvc-showcase总结更详细更通俗的讲解!!
首先进行准备工作:
- <mvc:annotation-driven/> 该片段默认就加载了 HttpMessageConverter接口的7种实现(如:StringHttpMessageConverter, MappingJacksonHttpMessageConverter 等)
- 进行请求提交时,需要指定MediaType,不同的MediaType对应的是不同的HttpMessageConverter接口实现类;见MappingJackson2HttpMessageConverter类只接受application/json类型的请求;
public class MappingJackson2HttpMessageConverter extends
AbstractHttpMessageConverter implements GenericHttpMessageConverter {
public MappingJackson2HttpMessageConverter() {
super(new MediaType[] {
new MediaType("application", "json", DEFAULT_CHARSET),
new MediaType("application", "*+json", DEFAULT_CHARSET) });
System.out.println("dsfsfsf");
objectMapper = new ObjectMapper();
prefixJson = false;
}
。。。。
}
如果进行请求提交和设置mediaType呢?见JS代码:设置AJAX提交的contentType属性即可;同时另外一点要注意:JSON字符串中的KEY,VALUE必须使用双引号,不可以使用单引号;
$.ajax({
type : "POST",///springmvc2/page1.mvc
url : "/springmvc2/json.mvc",//http://127.0.0.1:7001/springmvc2/json.mvc
contentType : "application/json",
data: '{"name":"1212","pwd":"我们"}' ,
dataType:'json',
success: function(msg){
alert( "Data Saved: " +msg.name );
}
});
$("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;
});
这些提交的信息如何能够查看呢?使用chrome内核的浏览器的开发者工具:
最后我们如何来控制我们的数据返回格式呢?
http://127.0.0.1:8010/messageconverters/json 它返回的XML格式的内容,其实是MarshallingHttpMessageConverter 转化后的结果,那我们如何指定转化的数据类型的(JSON OR XML)
http://127.0.0.1:8010/messageconverters/json.JSON;使用后缀指出他的返回数据格式
http://127.0.0.1:8010/messageconverters/json 在requestHeader设置
JS代码如下:
$("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;
});
本文中讲解了MappingJackson2HttpMessageConverter的使用,其他的 HttpMessageConverter都是类似的用法,具体可以看实例。另外官方的案例中的POM缺少相关的依赖内容,请下载我上传的压缩包,里面的JAR依赖是完整的。
总结本文内容如下:
1.如何使用HttpMessageConverter进行请求数据的转化
2.如何使用 HttpMessageConverter转化输出数据的格式
3.如何使用开发工具进行相关属性的查看