zoukankan      html  css  js  c++  java
  • 020 SpringMVC返回Json

    一:处理Json

    1.添加jar包

      添加json需要的包

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

      使用ResponseBody标签

     1 package com.spring.it.json;
     2 
     3 import java.util.Collection;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.ResponseBody;
     9 
    10 import com.spring.it.dao.EmployeeDao;
    11 import com.spring.it.enties.Employee;
    12 
    13 @Controller
    14 public class ReturnJsonHander {
    15     @Autowired
    16     private EmployeeDao employeeDao;
    17     
    18     @ResponseBody
    19     @RequestMapping(value="/testJson")
    20     public Collection<Employee> testJson() {
    21         return employeeDao.getAll();
    22     }
    23 }

    3.前段

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
     8 <script type="text/javascript">
     9     $(function(){
    10         $("#testJson").click(function(){
    11             var url=this.href;
    12             var args={};
    13             $.post(url,args,function(data){
    14                 for(var i=0;i<data.length;i++){
    15                     var id=data[i].id;
    16                     var lastName=data[i].lastName;
    17                     alert(id+":"+lastName);
    18                 }
    19             });
    20             return false;
    21         });
    22     })
    23 </script>
    24 <title>Insert title here</title>
    25 </head>
    26 <body>
    27     <br>
    28     <a href="emps">list emps</a>
    29     <br><br>
    30     <a href="testJson" id="testJson">Test Json</a>
    31 </body>
    32 </html>

    4.返回json

     1 [{
     2     "id": 1001,
     3     "lastName": "E-AA",
     4     "email": "aa@163.com",
     5     "gender": 1,
     6     "department": {
     7         "id": 101,
     8         "departmentName": "D-AA"
     9     },
    10     "birth": null,
    11     "salary": null
    12 }, {
    13     "id": 1002,
    14     "lastName": "E-BB",
    15     "email": "bb@163.com",
    16     "gender": 1,
    17     "department": {
    18         "id": 102,
    19         "departmentName": "D-BB"
    20     },
    21     "birth": null,
    22     "salary": null
    23 }, {
    24     "id": 1003,
    25     "lastName": "E-CC",
    26     "email": "cc@163.com",
    27     "gender": 0,
    28     "department": {
    29         "id": 103,
    30         "departmentName": "D-CC"
    31     },
    32     "birth": null,
    33     "salary": null
    34 }, {
    35     "id": 1004,
    36     "lastName": "E-DD",
    37     "email": "dd@163.com",
    38     "gender": 0,
    39     "department": {
    40         "id": 104,
    41         "departmentName": "D-DD"
    42     },
    43     "birth": null,
    44     "salary": null
    45 }, {
    46     "id": 1005,
    47     "lastName": "E-EE",
    48     "email": "ee@163.com",
    49     "gender": 1,
    50     "department": {
    51         "id": 105,
    52         "departmentName": "D-EE"
    53     },
    54     "birth": null,
    55     "salary": null
    56 }]

    5.请求

      

    二:HttpMessageConverter

    1.工作原理

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

      主要提供了两种途径:

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

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

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

    1.index

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
     8 <script type="text/javascript">
     9     $(function(){
    10         $("#testJson").click(function(){
    11             var url=this.href;
    12             var args={};
    13             $.post(url,args,function(data){
    14                 for(var i=0;i<data.length;i++){
    15                     var id=data[i].id;
    16                     var lastName=data[i].lastName;
    17                     alert(id+":"+lastName);
    18                 }
    19             });
    20             return false;
    21         });
    22     })
    23 </script>
    24 <title>Insert title here</title>
    25 </head>
    26 <body>
    27     <br>
    28     <a href="emps">list emps</a>
    29     <br><br>
    30     <a href="testJson" id="testJson">Test Json</a>
    31     <br><br>
    32     <form action="testHttpMessageConverter" method="post" enctype="multipart/form-data">
    33         File:<input type="file" name="file"/>
    34         Desc:<input type="text" name="desc"/>
    35         <input type="submit" value=Submit"">
    36     </form>
    37     
    38 </body>
    39 </html>

    2.处理方法

     1 package com.spring.it.json;
     2 
     3 import java.util.Collection;
     4 import java.util.Date;
     5 
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.stereotype.Controller;
     8 import org.springframework.web.bind.annotation.RequestBody;
     9 import org.springframework.web.bind.annotation.RequestMapping;
    10 import org.springframework.web.bind.annotation.ResponseBody;
    11 
    12 import com.spring.it.dao.EmployeeDao;
    13 import com.spring.it.enties.Employee;
    14 
    15 @Controller
    16 public class ReturnJsonHander {
    17     @Autowired
    18     private EmployeeDao employeeDao;
    19     
    20     @ResponseBody
    21     @RequestMapping(value="/testJson")
    22     public Collection<Employee> testJson() {
    23         return employeeDao.getAll();
    24     }
    25     
    26     @ResponseBody
    27     @RequestMapping(value="/testHttpMessageConverter")
    28     public String testHttpMessage(@RequestBody String body) {
    29         System.out.println("body:"+body);
    30         return "helloWorld"+new Date();
    31     }
    32 }

    3.效果

      

      

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

    1.请求

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

    2.处理方法

     1 package com.spring.it.json;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.util.Collection;
     6 import java.util.Date;
     7 
     8 import javax.servlet.ServletContext;
     9 import javax.servlet.http.HttpSession;
    10 
    11 import org.springframework.beans.factory.annotation.Autowired;
    12 import org.springframework.http.HttpHeaders;
    13 import org.springframework.http.HttpStatus;
    14 import org.springframework.http.ResponseEntity;
    15 import org.springframework.stereotype.Controller;
    16 import org.springframework.web.bind.annotation.RequestBody;
    17 import org.springframework.web.bind.annotation.RequestMapping;
    18 import org.springframework.web.bind.annotation.ResponseBody;
    19 
    20 import com.spring.it.dao.EmployeeDao;
    21 import com.spring.it.enties.Employee;
    22 
    23 @Controller
    24 public class ReturnJsonHander {
    25     @Autowired
    26     private EmployeeDao employeeDao;
    27     
    28     @ResponseBody
    29     @RequestMapping(value="/testJson")
    30     public Collection<Employee> testJson() {
    31         return employeeDao.getAll();
    32     }
    33     
    34     @ResponseBody
    35     @RequestMapping(value="/testHttpMessageConverter")
    36     public String testHttpMessage(@RequestBody String body) {
    37         System.out.println("body:"+body);
    38         return "helloWorld"+new Date();
    39     }
    40     
    41     @RequestMapping("/testResponseEntity")
    42     public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws Exception{
    43         byte[] body=null;
    44         ServletContext context=session.getServletContext();
    45         InputStream in=context.getResourceAsStream("/file/ADC.txt");
    46         body=new byte[in.available()];
    47         in.read(body);
    48         HttpHeaders headers=new HttpHeaders();
    49         headers.add("Content-Disposition", "attament;fileName=ADC.txt");
    50         HttpStatus status=HttpStatus.OK;
    51         ResponseEntity<byte[]> response=new ResponseEntity<>(body,headers,status);
    52         return response;
    53     }
    54     
    55     
    56 }

    3.效果

      浏览器上:

      

  • 相关阅读:
    iOS 多线程/GCD
    iOS推送通知的实现步骤
    Swift中文教程-学习
    设计模式——观察者模式
    SSM学习
    Servlet 学习
    java基础
    DOM中节点
    会议管理系统设计
    springboot与thymeleaf 整合
  • 原文地址:https://www.cnblogs.com/juncaoit/p/8733701.html
Copyright © 2011-2022 走看看