SpringMVC 学习笔记(九)- 处理 JSON:使用 HttpMessageConverter

1. 处理 JSON

1. 主要步骤

  1. 加入 jar 包
  2. 编写目标方法,使其返回 JSON 对应的对象或集合
  3. 在方法上添加 @ReponseBody 注解

2. 具体实现

1. 在 SpringMVCTest 中加入

@ResponseBody
	@RequestMapping("/testJson")
	public Collection<Employee> testJson(){
		return employeeDao.getAll();
	}

2. 视图 index.jsp 中加入

<a href="testJson" id="testJson">Test Json</a>
	<br><br>

2. HttpMessageConverter 原理图

image