zoukankan      html  css  js  c++  java
  • SpringMVC如何接收application/json内容编码类型的参数?

      在上代码之前,有必要先说说@ResquestBody注解的含义:

      1、官方解释如下:

    Annotation indicating a method parameter 
    should be bound to the body of the web request.
    The body of the request is passed through an {@link HttpMessageConverter}
    to resolve the method argument depending on the content type of the request.
    意思大概是:用该注解标识的方法的参数,会和web请求体绑定。
    http消息转换器会根据content-type的设置将请求体解析,从而初始化该方法的参数。

      2、另外还需解释一下使用的场景

    GET、POST方式提交的请求:

    Content-type:

    1、application/x-www-form-urlencoded:@RequestBody不是必须加的

    2、mutipart/form-data:@RequestBody不能处理这种格式

    3、其他格式,比如application/json,application/xml等,必须使用@RequestBody来处理

    PUT方式提交的请求:

    以上1和3的场景都是必须使用@RequestBody来处理的,2场景也是不支持的

      3、前端代码如下:(这里必须将JSON对象使用JSON.stringify()转为JSON字符串再传递,否则后台接收不到值)

    $.ajax({
        url:"../../Notice/LoadForm.do",
        type:"post",
        contentType:"application/json;charset=UTF-8",
        data:JSON.stringify({"id":"1","title":"标题"})
    });

      4、后台接收代码示例

    @RequestMapping(value="Notice/LoadForm")
    @ResponseBody
    public ResultJO loadForm(@RequestBody Notice notice){
    
    }
     
  • 相关阅读:
    Sphinx安装流程及配合PHP使用经验
    使用HTML5视频事件示例
    Centos6.5下编译安装mysql 5.6
    AES加密
    ab参数详解 – 压力测试
    vim 常用快捷键
    telnet操作memcache
    如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作
    array_map 巧替 foreach
    mac brew安装mysql
  • 原文地址:https://www.cnblogs.com/anai/p/4272573.html
Copyright © 2011-2022 走看看