使用json
spring使用Jackson步骤:
1、引入3个包(pom.xml)
1 <dependency> 2 <groupId>com.fasterxml.jackson.core</groupId> 3 <artifactId>jackson-annotations</artifactId> 4 <version>${jackson.version}</version> 5 </dependency> 6 <dependency> 7 <groupId>com.fasterxml.jackson.core</groupId> 8 <artifactId>jackson-core</artifactId> 9 <version>${jackson.version}</version> 10 </dependency> 11 <dependency> 12 <groupId>com.fasterxml.jackson.core</groupId> 13 <artifactId>jackson-databind</artifactId> 14 <version>${jackson.version}</version> 15 </dependency>
这里的spring版本:<spring.version>4.2.4.RELEASE</spring.version>
2、启动<mvn:annotation-driver/>注解(spring.xml)
1 <!-- 使用json需要启动的配置 --> 2 <mvc:annotation-driven />
3、在web层标注@ResponseBody注解
1 @Controller 2 public class EmployeeHandler { 3 @Autowired 4 private EmployeeDao employeeDao; 5 @Autowired 6 private DepartmentDao departmentDao; 7 @RequestMapping(value="/updateEmployee",method=RequestMethod.PUT) 8 @ResponseBody 9 public Collection<Employee> updateEmployee(@RequestParam("id") Integer id, 10 @RequestParam("email") String email){ 11 Employee employee = employeeDao.get(id); 12 employee.setEmail(email); 13 Map<Integer, Employee> es = EmployeeDao.employees; 14 es.put(id, employee); 15 return employeeDao.getAll(); 16 } 17 }
为了更加详细的说明如何使用json,在“springmvc之使用RESTFUL-CRUD”一文中实现了整体应用。
一般需要将显示出的内容翻译为jason格式,在线翻译json格式的网址为:http://www.bejson.com/
每天坚持进步一点点。