@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);  
    }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM