测试环境:spring 3.1.1 + Hibernate 3.3.2 + Jackson-all-1.8.0
spring 配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- Bean头部 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <context:component-scan base-package="com.ciaos.controller"/> <mvc:annotation-driven /> <!-- Configure to plugin JSON as request and response in method handler --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="jsonMessageConverter"/> </list> </property> </bean> <!-- Configure bean to convert JSON to POJO and vice versa --> <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> </bean> </beans>
controller使用示例如下:
package com.ciaos.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ciaos.dao.Account;
import com.ciaos.dao.AccountDAO;
import com.ciaos.dao.Hobby;
import com.ciaos.dao.HobbyDAO;
@Controller
public class DemoController {
@RequestMapping(value ="/rest.json")
@ResponseBody
public Object json(@RequestParam Integer id){
AccountDAO dao = new AccountDAO();
Account account = dao.findById(id);
Map<String, Object> response = new HashMap<String, Object>();
response.put("result", account);
return response;
}
@RequestMapping(value ="/rest.jsonp", produces = "text/javascript;charset=UTF-8")
@ResponseBody
public Object jsonp(@RequestParam("callback") String callback, @RequestParam Integer id){
HobbyDAO dao = new HobbyDAO();
List<Hobby> list = dao.findAll();
Map<String, Object> response = new HashMap<String, Object>();
response.put("result", list);
ObjectMapper mapper = new ObjectMapper();
String resultstr = null;
try {
resultstr = mapper.writeValueAsString(response);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return callback + "(" + resultstr + ")";
}
}
POJO类增加@JsonIgnore防止递归错误
//Account.java (隐藏不需要的字段)
// Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@JsonIgnore
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
//Hobby.java (防止递归)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "accountid")
@JsonIgnore
public Account getAccount() {
return this.account;
}