@RequestBody和@RequestParam区别

@RequestParam

用来处理请求头Content-Type: 为 application/x-www-form-urlencoded编码的内容。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)

RequestParam可以接受简单类型的属性,也可以接受对象类型。 
实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。

@RequestBody

用来处理请求头Content-Type: 为 application/json编码的内容,明确的告诉服务器发送的内容是json。因为需要读取body中内容,所以只能接受post请求。

    $.ajax({  
        type: "post",  
        contentType:"application/json",  
        url: "repairs/saveDispatches",  
        data: JSON.stringify(dispatchesDTO),  
        success: function(data){  
            if(!data.success){  
              alertError("派工失败");  
            }else{  
            alertSuccess("派工成功");  
        }  
    })  

后台代码如下:

    @RequestMapping("/repairs/saveDispatches")  
    public void saveDispatches(@RequestBody DispatchesDTO dispatchesDTO,  
            HttpServletResponse response) throws IOException {  
      
        dispatchesService.saveDispatches(dispatchesDTO);  
        success(response);  
    }  

posted on 2019-11-04 23:31  小破孩楼主  阅读(686)  评论(0编辑  收藏  举报