获取JSON数据
方式一、使用JSON工具将对象序列化成Json,常用工具有Jackson,fastjson,gjson,然后利用HttpServletResponse获取response.getOutputStream(),或者PrintWriter()fhf接输出
以Jackson为例:
引包
<!-- json依赖包 -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency>
前端:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="${path}/resource/js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$(".testJson").click(function(){
var url=this.href;
alert(url)
var args={};
//ajax,url:目标地址 ; args:带出去的参数 ;data:目标方法返回的值
$.post(url,args,function(data){
for(var i=0; i<data.length;i++){
var userid=data[i].id;
var username=data[i].name;
alert(userid+":"+username);
}
});
return false;
});
});
</script>
<meta charset="utf-8">
<title>testJson</title>
</head>
<body>
<br><br><br>
<a class="testJson" href="${path}/getJson">传统方式获取json数据测试</a>
<br><br>
<a class="testJson" href="${path}/getJson2">MVC方式获取json数据测试</a>
</body>
</html>
后台:
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restfullDemo.model.User;
import com.restfullDemo.service.UserService;
/*在调用Json工具对对像进行 Json对像转换时,如明对像有关联的对像是 赖加载的,那必须在数据库文件中,要设lazy="false" ,或在对像的实体内中加入@JsonIgnoreProperties忽略相关的属性*/
@Controller
public class TestJsonController {
@Autowired
private UserService userService;
@RequestMapping (value="/testJson")
public String jsonView() {
return "testJson";
}
/* 传统方式来返回json */
@RequestMapping(value="/getJson",method = RequestMethod.POST)
public void getJson(HttpServletResponse resp,PrintWriter out) throws JsonProcessingException {
resp.setContentType("application/json;charset=utf-8");
/* 清除缓存中的对应数据 */
resp.setHeader("pragma", "no-cache");
resp.setHeader("cache-control", "no-cache");
List<User> users=userService.getAllUsers();
ObjectMapper mapper=new ObjectMapper();
String jsonStr=mapper.writeValueAsString(users);
out.write(jsonStr);
out.close();
}
}
方式二、使用springmvc 中@ResponseBody标签
后台:
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.restfullDemo.model.User;
import com.restfullDemo.service.UserService;
/*在调用Json工具对对像进行 Json对像转换时,如明对像有关联的对像是 赖加载的,那必须在数据库文件中,要设lazy="false" ,或在对像的实体内中加入@JsonIgnoreProperties忽略相关的属性*/
@Controller
public class TestJsonController {
@Autowired
private UserService userService;
@RequestMapping (value="/testJson")
public String jsonView() {
return "testJson";
}
/* mvc方式来返回json */
@ResponseBody
@RequestMapping(value="/getJson2",method = RequestMethod.POST)
public List<User> getJson2(){
List<User> users=userService.getAllUsers();
return users;
}
}
注意,在调用Json工具对对像进行 Json对像转换时,如明对像有关联的对像是 赖加载的,那必须在数据库文件中,要设lazy="false" ,或在对像的实体内中加入@JsonIgnoreProperties忽略相关的属性