Java中的@RequestBody和@RequestParam注解的用法


1.spring的RequestParam注解接收的參數是來自於requestHeader中,即請求頭,也就是在url中,格式為xxx?username=123&password=456,

而RequestBody注解接收的參數則是來自於requestBody中,即請求體中。

2.如果是從js通過ajax請求傳送json字符串到controller層,在接受數據的形參用@RequestBody注解時,ajax請求時內容類型為contentType:"application/json",且將要傳送的數據從json對象轉為json字符串data: JSON.stringify(param);

3.如果在ajax的請求地址中帶有參數,則在controller層中可以加@RequestParam注解獲取參數,常用於get請求,也可以不加,加的話因為@RequestParam是key-value類型,那地址的參數中必須有形參中的key值

4.ajax請求參數中也可以有數組類型,如例子中的taskTemplateValueList

5.遇到復雜的數據格式,一般來說還是json比較好處理,即用@RequestBody接收

js:

/**
* 點擊預覽按鈕
*/
function smsPreLook() {
var templateId = $('#templateId').val();
var templateContent = $('#templateContent').val();
if (templateContent == null || templateContent == '') {
msgInfoModal("提示",'請編輯文本內容');
return;
}

//關鍵字
var taskTemplateValueList=[];
var checkValue=true;
$("tr[name='taskTemplateValueListTr']").each(function(){
var paramName=$(this).find('input[name=paramName]').val();
var paramValue=$(this).find('input[name=paramValue]').val();

var taskTemplateValue={};
taskTemplateValue.paramName = paramName;
taskTemplateValue.paramValue = paramValue;

taskTemplateValueList.push(taskTemplateValue);

if (paramValue == null || paramValue == '') {
checkValue=false;
}
});
if (!checkValue) {
msgInfoModal("提示",'請輸入關鍵字的替換內容');
return;
}

var smilResourceId=$('#smilResourceId').val().trim();
var isdetail=false;

var param = {
smilResourceId: smilResourceId,
smsMsgContent:templateContent,
templateContent:templateContent,
templateId:templateId,
taskTemplateValueList:taskTemplateValueList//數組類型
};
$.ajax({
url: WEB_ROOT + "/mall/task/preLook?isdetail="+isdetail,
type: "html",//返回值類型
data: JSON.stringify(param),//將要傳送的數據從json對象轉為json字符串
contentType:"application/json",//定義數據內容類型為"application/json"
     method: "post",
        success: function (page) {
$("#preLook").html(page);
$('#preLookModal').modal('show');
}
});
}

java:

/**
     * 預覽顯示
     */
    @RequestMapping(value = "/preLook")
//如果在isdetail參數前有@RequestParam(value="isdetail",required = true),則在從js傳來的請求地址后的參數必須有isdetail作為key值的參數,required則控制該參數的value值是否必傳
public String examineInit(Model model, @RequestBody TaskVO taskVO, Boolean isdetail) throws Exception {
AuthStaffVO staff = StaffUtil.getStaffVO(session);
    if (isdetail && taskVO.getId() != null) {
taskVO = itTaskSV.selectTaskVOById(taskVO.getId());
} else {
//校驗文本內容的權限(部分自定義的話,只能用模板;完全自定義可以不用模板)
if (TaskConstant.TASK_RIGHT_1.equals(staff.getTaskRight())) {
//必須使用模板
if (taskVO.getTemplateId() == null) {
return null;// R.failure("請選擇模板~")
}
setTaskSmsMsgContentByTemplate(taskVO);
} else if (TaskConstant.TASK_RIGHT_2.equals(staff.getTaskRight())) {
//可以不用模板
if (taskVO.getTemplateId() != null) {
setTaskSmsMsgContentByTemplate(taskVO);
} else {
taskVO.setSmsMsgContent(taskVO.getTemplateContent());
}
} else {
return null;//R.failure("您當前沒有權限進行此操作哦~");
}
}
List<TaskVO> taskVOList = new ArrayList<TaskVO>();
try {
taskVOList = itTaskSV.selectTaskVOList(taskVO);
} catch (Exception e) {
log.error("短信任務管理(后台)--數字短信預覽異常", e);
}
model.addAttribute("taskVOList", taskVOList);
return "homeMain/myCenter/task/mall-smsPreLook";
}


免責聲明!

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



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