使用以下步骤让controller函数返回json字符串:
1、controller函数,在@RequestMapping(...) 后添加 @ResponseBody注解
2、controller函数返回值为 要转换为json字符串的model类型。 当本函数返回值为null时,web程序返回的http内容为空,而不是{}
3、给pom.xml添加以下两个依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
示例:
@RequestMapping(value="/getuserjson.do",method=RequestMethod.GET)
@ResponseBody
public User getUserJson(HttpSession httpSession){
User user=(User)httpSession.getAttribute("user");
if(user==null)
return null;
else
return userService.userLogin(user);
}