一:处理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.请求

  

二: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.效果

  

  

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

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的更多相关文章

  1. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  2. 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  3. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  4. SpringMVC返回Json,自定义Json中Date类型格式

    http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...

  5. 配置SpringMVC返回JSON遇到的坑

    坑一:官方网站下载地址不明朗,最后找了几个下载地址:http://wiki.fasterxml.com/JacksonDownload Jackson2.5下载地址:jackson2.5.0.jar ...

  6. SpringMVC返回JSON数据时日期格式化问题

    https://dannywei.iteye.com/blog/2022929 SpringMVC返回JSON数据时日期格式化问题 博客分类: Spring   在运用SpringMVC框架开发时,可 ...

  7. spring入门(七)【springMVC返回json串】

    现在多数的应用为了提高交互性多使用异步刷新,即在不刷新整个页面的情况下,只刷新局部,局部刷新用得最多就是ajax,ajax和后台进行交互的数据格式使用的最多的是JSON,这里简单描述,在springm ...

  8. springmvc返回json字符串中文乱码问题

    问题: 后台代码如下: @RequestMapping("menuTreeAjax") @ResponseBody /** * 根据parentMenuId获取菜单的树结构 * @ ...

  9. SpringMVC返回JSON方案

    SpringMVC已经大行其道.一般的,都是返回JSP视图.如果需要返回JSON格式,我们大都掌握了一些方法. 在ContentNegotiatingViewResolver之前,一般使用XmlVie ...

随机推荐

  1. Oracle——存储过程简单入门实例

    1.连接plsql developer,打开一个SQL Window 2.SQL Window中创建表user_info -- Create table create table USER_INFO ...

  2. B. ZgukistringZ

    题目链接:http://codeforces.com/contest/551/problem/B 题目大意:给你三个字符串,s1,s2,s3.  s1任意两个字符串之间可以互相交换. 问,在s1中s2 ...

  3. yum和rpm的区别

    rpm是由红帽公司开发的软件包管理方式,使用rpm我们可以方便的进行软件的安装.查询.卸载.升级等工作.但是rpm软件包之间的依赖性问题往往会很繁琐,尤其是软件由多个rpm包组成时.Yum(全称为 Y ...

  4. Three.js基础探寻二——正交投影照相机

    本篇主要介绍照相机中的正交投影照相机. 第一篇传送门:Three.js基础探寻一 1.照相机 图形学中的照相机定义了三维空间到二维屏幕的投影方式. 针对投影方式照相机分为正交投影照相机和透视投影照相机 ...

  5. Codeplex最流行25个开源项目

    1. VMukti富媒体协作平台 2. Microsoft SQL Server Product Samples: Engine 3. Patterns & Practices: Enterp ...

  6. 图文并茂的Python教程-numpy.pad

    图文并茂的Python教程-numpy.pad np.pad()常用与深度学习中的数据预处理,可以将numpy数组按指定的方法填充成指定的形状. 声明: 需要读者了解一点numpy数组的知识np.pa ...

  7. Pytorch 之 backward

    首先看这个自动求导的参数: grad_variables:形状与variable一致,对于y.backward(),grad_variables相当于链式法则dz/dx=dz/dy × dy/dx 中 ...

  8. UML和模式应用5:细化阶段(8)---逻辑架构和UML包图

    1.前言 本章是从面向分析的工作过度到软件设计 典型的OO系统设计的基础是若干架构层,如UI层.应用逻辑(领域)层 本章简要考察逻辑分层架构和相关UML表示法 2.逻辑架构和层 逻辑架构 逻辑架构是软 ...

  9. kafka系列六、java管理kafka Topic

    package com.example.demo.topic; import kafka.admin.AdminUtils; import kafka.admin.RackAwareMode; imp ...

  10. 量化投资与Python之NumPy

      数组计算 NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础.NumPy的主要功能:ndarray,一个多维数组结构,高效且节省空间无需循环对整组数据进行快速运算的 ...