SpringMVC框架 -- json数据交互

一.JSON简单介绍:

参考笔记:JSON简单快速入门

二.json数据交互

请求json 输出json 需要请求数据为json,需要在前端转为json不太方便。不常用
请求key/value、输出json。常用

SpringMVC框架 -- json数据交互_第1张图片
  • 1.环境搭建

下载jar包
jackson-core-asl-1.9.13.jar
jackson-mapper-asl-1.9.13.jar


    
      com.fasterxml.jackson.core
      jackson-databind
      2.7.2
    
    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.13
    
  • 2.配置json转换器


    
    
    
    
    

  • 注意如果使用则不用定义上边的内容。

三测试

  • 输入json串,输出json串
 function requestJson(){
        $.post({
            type:'post',
            url:'/json/requestJson',
            contentType:'application/json;charset=utf-8',
            data:'{"userName":"小明","passWord":"123456"}'
        },function (data) {
            console.log(data);
        });
    }

jsonControllar.java处理

package com.huan.web.json;


import com.huan.entity.User;
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;

@Controller
@RequestMapping("/json")
public class jsonControllar {
    //将接受过来的json对象处理完,转为json返回
    @RequestMapping("/requestJson")
    public @ResponseBody User requestJson(@RequestBody User user){

        return user;
    }
}
SpringMVC框架 -- json数据交互_第2张图片
SpringMVC框架 -- json数据交互_第3张图片
  • 输入key/value,输出json串
function responseJson(){
        $.post({
            type:'post',
            url:'/json/responseJson',
            data:"userName=小明&passWord=123456"
        },function (data) {
            console.log(data);
        });

jsonControllar.java处理

package com.huan.web.json;


import com.huan.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/json")
public class jsonControllar {
    //将接受过来的key/value对象处理完,转为json返回
    @RequestMapping("/responseJson")
        public @ResponseBody User responseJson(User user){
            return user;
        }
}
SpringMVC框架 -- json数据交互_第4张图片
SpringMVC框架 -- json数据交互_第5张图片

你可能感兴趣的:(SpringMVC框架 -- json数据交互)