020 SpringMVC返回Json

时间:2021-07-26 05:05:31

一:处理Json

1.添加jar包

  添加json需要的包

2.后端返回json对用的对象或者集合

  使用ResponseBody标签

 package com.spring.it.json;

 import java.util.Collection;

 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.ResponseBody; import com.spring.it.dao.EmployeeDao;
import com.spring.it.enties.Employee; @Controller
public class ReturnJsonHander {
@Autowired
private EmployeeDao employeeDao; @ResponseBody
@RequestMapping(value="/testJson")
public Collection<Employee> testJson() {
return employeeDao.getAll();
}
}

3.前段

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#testJson").click(function(){
var url=this.href;
var args={};
$.post(url,args,function(data){
for(var i=0;i<data.length;i++){
var id=data[i].id;
var lastName=data[i].lastName;
alert(id+":"+lastName);
}
});
return false;
});
})
</script>
<title>Insert title here</title>
</head>
<body>
<br>
<a href="emps">list emps</a>
<br><br>
<a href="testJson" id="testJson">Test Json</a>
</body>
</html>

4.返回json

 [{
"id": 1001,
"lastName": "E-AA",
"email": "aa@163.com",
"gender": 1,
"department": {
"id": 101,
"departmentName": "D-AA"
},
"birth": null,
"salary": null
}, {
"id": 1002,
"lastName": "E-BB",
"email": "bb@163.com",
"gender": 1,
"department": {
"id": 102,
"departmentName": "D-BB"
},
"birth": null,
"salary": null
}, {
"id": 1003,
"lastName": "E-CC",
"email": "cc@163.com",
"gender": 0,
"department": {
"id": 103,
"departmentName": "D-CC"
},
"birth": null,
"salary": null
}, {
"id": 1004,
"lastName": "E-DD",
"email": "dd@163.com",
"gender": 0,
"department": {
"id": 104,
"departmentName": "D-DD"
},
"birth": null,
"salary": null
}, {
"id": 1005,
"lastName": "E-EE",
"email": "ee@163.com",
"gender": 1,
"department": {
"id": 105,
"departmentName": "D-EE"
},
"birth": null,
"salary": null
}]

5.请求

  020 SpringMVC返回Json

二:HttpMessageConverter

1.工作原理

  使用HttpMessageConverter<T>将请求信息转化并绑定到处理方法的入参中,或将响应结果转化为对应类型的响应信息。

  主要提供了两种途径:

  @RequestBody与@ResponseBody对处理方法进行标注

  HttpEntity与ResponseEntity作为处理方法的入参或返回值

三:案例--上传--注解

1.index

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#testJson").click(function(){
var url=this.href;
var args={};
$.post(url,args,function(data){
for(var i=0;i<data.length;i++){
var id=data[i].id;
var lastName=data[i].lastName;
alert(id+":"+lastName);
}
});
return false;
});
})
</script>
<title>Insert title here</title>
</head>
<body>
<br>
<a href="emps">list emps</a>
<br><br>
<a href="testJson" id="testJson">Test Json</a>
<br><br>
<form action="testHttpMessageConverter" method="post" enctype="multipart/form-data">
File:<input type="file" name="file"/>
Desc:<input type="text" name="desc"/>
<input type="submit" value=Submit"">
</form> </body>
</html>

2.处理方法

 package com.spring.it.json;

 import java.util.Collection;
import java.util.Date; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.spring.it.dao.EmployeeDao;
import com.spring.it.enties.Employee; @Controller
public class ReturnJsonHander {
@Autowired
private EmployeeDao employeeDao; @ResponseBody
@RequestMapping(value="/testJson")
public Collection<Employee> testJson() {
return employeeDao.getAll();
} @ResponseBody
@RequestMapping(value="/testHttpMessageConverter")
public String testHttpMessage(@RequestBody String body) {
System.out.println("body:"+body);
return "helloWorld"+new Date();
}
}

3.效果

  020 SpringMVC返回Json

  020 SpringMVC返回Json

四:案例--下载--处理方法

1.请求

  <a href="testResponseEntity">Test ResponseEntity</a>

2.处理方法

 package com.spring.it.json;

 import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Date; import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.spring.it.dao.EmployeeDao;
import com.spring.it.enties.Employee; @Controller
public class ReturnJsonHander {
@Autowired
private EmployeeDao employeeDao; @ResponseBody
@RequestMapping(value="/testJson")
public Collection<Employee> testJson() {
return employeeDao.getAll();
} @ResponseBody
@RequestMapping(value="/testHttpMessageConverter")
public String testHttpMessage(@RequestBody String body) {
System.out.println("body:"+body);
return "helloWorld"+new Date();
} @RequestMapping("/testResponseEntity")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws Exception{
byte[] body=null;
ServletContext context=session.getServletContext();
InputStream in=context.getResourceAsStream("/file/ADC.txt");
body=new byte[in.available()];
in.read(body);
HttpHeaders headers=new HttpHeaders();
headers.add("Content-Disposition", "attament;fileName=ADC.txt");
HttpStatus status=HttpStatus.OK;
ResponseEntity<byte[]> response=new ResponseEntity<>(body,headers,status);
return response;
} }

3.效果

  浏览器上:

  020 SpringMVC返回Json