springmvc的json数据交互

准备

@RequestBody

作用:

@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容(json数据)转换为java对象并绑定到Controller方法的参数上。

@ResponseBody

作用:

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

需求:通过jsp页面请求json,相应json.

本例中:

@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象进行绑定

@ResponseBody注解实现将Controller方法返回java对象转换为json响应给客户端

实现步骤

1 加入jar包

2 准备一个jsp页面

 书写一个jsp页面,用于向服务器提交json数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>json数据交互</title>

<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
    $(function(){
        //alert();
        //$.post(); //写回的可以指定是json字符串,不能发送json字符串
        var params ='{"id": 1,"name": "测试商品","price": 99.9,"detail": "测试商品描述","pic": "123456.jpg"}';
        $.ajax({
            url : "${pageContext.request.contextPath }/json.action",
            data : params,
            contentType : "application/json;charset=UTF-8", //发送数据的格式
            type : "post",
            dataType : "json", //回调
            success : function(data){
                alert(data.name);
            }
        });
    });
</script>
</head>
<body>
    <h1>这也页面用于json数据交互的练习</h1>
</body>
</html>

 3 JsonController编写

package com.test.springmvc.controller;

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.test.springmvc.pojo.Items;

@Controller
public class JsonController {

    /*
     * json数据交互
     */
    @RequestMapping("/json.action")
    public @ResponseBody Items jsonTest(@RequestBody Items items){
        System.out.println(items);
        return  items;
    }
}

4 Items类

package com.test.springmvc.pojo;

import java.util.Date;

public class Items {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic == null ? null : pic.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail == null ? null : detail.trim();
    }
}
View Code

3 配置json转换器

 本例中并不需要配置json转换器。因为我使用的注解驱动开发。

如果不使用注解驱动<mvc:annotation-driven />,就需要给处理器适配器配置json转换器,参考之前学习的自定义参数绑定。

springmvc.xml配置文件中,给处理器适配器加入json转换器:

<!--处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
          <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
        </list>
    </property>
</bean>
原文地址:https://www.cnblogs.com/jepson6669/p/9042521.html