(九)springmvc之json的处理(服务端发送json数据到客户端)

 

一、json处理方法有两种

 

    1:导入Spring需要json的jar包。(本例使用)

        使用@ResponseBody该注解用于将Controller的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过         Response响应给客户端     

    

     2:springmvc.xml中配置转换json的配置。

   

二、案例一:使用@ResponseBody

  •    依赖的包

    <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.1</version>
        </dependency>

 

index.jsp

<script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.json-2.4.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.form.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function ajax_json_1(){
    var getURL = "<%=request.getContextPath()%>/jsonController/dogList?date="+new Date()+"";
    jQuery.get(getURL,function(jsonList){
        for(var i = 0;i<jsonList.length;i++){
            var dogBean = jsonList[i];
            window.alert(dogBean.dogid);
        }
    },"json");
}
</script>
</head>
<body>

    <a href="javascript:void(0);" onclick="ajax_json_1();">jQuery来访问_SpringMVC返回json对象</a>
</body>

controller

package com.shyroke.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shyroke.bean.DogBean;

@Controller
@RequestMapping(value="/jsonController")
public class JsonController {

    
    @RequestMapping(value="/dogList")
    @ResponseBody
    public List<DogBean> getListDog(){
        List<DogBean> dogList=new ArrayList<DogBean>();
        DogBean dog1=new DogBean();
        dog1.setDogid("1");
        dog1.setDogname("dog1");
        
        DogBean dog2=new DogBean();
        dog2.setDogid("2");
        dog2.setDogname("dog2");
        
        dogList.add(dog1);
        dogList.add(dog2);
        return dogList;
    }
    
}

 

三、案例二:服务端使用原始的方式返回json数据,方法不需要有返回值

  • 同样需要引入上例的包

<script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.json-2.4.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.form.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">function ajax_json_2(){
    var getURL = "<%=request.getContextPath()%>/jsonController/dogBean?date="+new Date()+"";
    jQuery.get(getURL,function(jsonObj){
        window.alert(jsonObj.dogid + "\t\t" + jsonObj.dogname);
    },"json");
}

</script>
</head>
<body>
    <a href="javascript:void(0);" onclick="ajax_json_2();">jQuery来访问_原始返回json对象</a>
</body>

controller

package com.shyroke.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.shyroke.bean.DogBean;

@Controller
@RequestMapping(value="/jsonController")
public class JsonController {
 
    @RequestMapping(value="dogBean")
    public void GetDog(HttpServletRequest request,HttpServletResponse response) throws Exception{
        PrintWriter out=response.getWriter();
        
        DogBean dog1=new DogBean();
        dog1.setDogid("1");
        dog1.setDogname("dog1");
        
        /**
         * 转成json字符串。
         */
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        // 配置mapper忽略空属性
        objectMapper.setSerializationInclusion(Include.NON_EMPTY);

        String jsonStr = objectMapper.writeValueAsString(dog1);
        
        out.print(jsonStr);
        out.flush();
        out.close();
    }
}

 

posted @ 2017-11-03 15:13  shyroke、  阅读(577)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~