加入 jar 包
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
编写目标方法,使其返回 JSON 对应的对象或集合
@ResponseBody //SpringMVC 对 对 JSON 的支持
@RequestMapping("/testJSON")
public Collection<Employee> testJSON(){
return employeeDao.getAll();
}
增加页面代码:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#testJSON").click(function(){
var url = this.href ;
var args = {};
$.post(url,args,function(data){
for(var i=0; i<data.length; i++){
var id = data[i].id;
var lastName = data[i].lastName ;
alert(id+" - " + lastName);
}
});
return false ;
});
});
</script>
</head>
<body>
<a href="empList">To Employee List</a>
<br><br>
<a id="testJSON" href="testJSON">testJSON</a>
</body>
</html>
HttpMessageConverter 是 Spring3.0 新添加的一个接口, 负责将请求信息转换为一个对象(类型为 T ),为 将对象(类型为 T )输出为响应信息
HttpMessageConverter 接口定义的方法:
DispatcherServlet 默认装配 RequestMappingHandlerAdapter ,而 RequestMappingHandlerAdapter 默认装配如下 HttpMessageConverter:
加入 jackson jar 包后, RequestMappingHandlerAdapter装配的 HttpMessageConverter 如下:
默认情况下数组长度是 6 个;增加了 jackson 的包,后多一个MappingJackson2HttpMessageConverter
使用 HttpMessageConverter 将请求信息转化并绑定到处理方法的入参中或将响应结果转为对应类型的响应信息,Spring 提供了两种途径:
当控制器处理方法使用到 @RequestBody/@ResponseBody 或HttpEntity/ResponseEntity 时, Spring 的 首先根据请求头或响应头的 Accept 属性选择匹配的 HttpMessageConverter, 进而根据参数类型或泛型类型的过滤得到匹配的HttpMessageConverter, 若找不到可用的 HttpMessageConverter 将报错
@RequestBody 和 和 @ResponseBody 不需要成对出现
Content-Disposition:attachment; filename=abc.pdf
@RequestBody @ResponseBody
<form action="testHttpMessageConverter" method="post" enctype="multipart/form-data">
文件: <input type="file" name="file"/><br><br>
描述: <input type="text" name="desc"/><br><br>
<input type="submit" value="提交"/>
</form>
@ResponseBody // @ResponseBody:是将内容或对象作为 Http 响应正文返回
@RequestMapping("/testHttpMessageConverter")
// @RequestBody:是将 Http 请求正文插入方法中,修饰目标方法的入参
public String testHttpMessageConverter(@RequestBody String body){
System.out.println("body="+body);
return "Hello," + new Date(); //不再查找跳转的页面
}
HttpEntity ResponseEntity
/files/abc.txt 准备一个下载的文件
<a href="testResponseEntity">abc.txt</a>
@RequestMapping("testResponseEntity")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{
ServletContext servletContext = session.getServletContext();
InputStream resourceAsStream = servletContext.getResourceAsStream("/files/abc.txt");
byte[] body = new byte[resourceAsStream.available()] ;
resourceAsStream.read(body);
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=abc.txt");
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(body, headers, statusCode);
return responseEntity ;
}