什麼js對象,json對象,json字符串?它們又長什麼樣呢?
js對象:jsObj
json對象:jsonObj
json字符串:jsonString
ajax如何發post?
1:定義json對象
var obj = {"factoryId":Id};
2:寫$.ajax$.ajax({
type:”POST”,
url: “/ASW/wxcurrent.html”,
data:JSON.stringify(jsonObj),
dataType: “json”,
contentType:”application/json”,
success:function(result){
console.log(success);
},
error:function(){
alert(“error”);
}
})
解釋一波:ajax可以發送三種格式的請求+數據
:post+json字符串(最常用的方式)
:post+json對象
:get+查詢字符串
3.@requestBody接收json字符串
點擊:Understanding @RequestBody
網上幾乎博客的思路都是:在Java服務端見一個bean或java對象類,MappingJacksonHttpMessageConverter會將json數據轉換成這個java對象來屬性值。
官方對@requestBody的解釋是:
(1)For access to the HTTP request body. Body content is converted to the declared method argument type using HttpMessageConverters.
看這個下面這個舉例,可以知道可以直接獲取基本數據類型String,並不是只可以獲取引用類型(java對象)
比如:我要獲取factortId的值,我的方法參數類型就得是擁有factortId屬性的Factory類,
@RequestMapping(value = "/wxcurrent",method=RequestMethod.POST,consumes="application/json")
@ResponseBody
public void wxfactoryCur(@RequestBody Factory factory, HttpServletResponse response) {
System.out.println("factory:"+factory);
System.out.println("factoryId:"+factory.getFactoryId());
}
Factory實體類
package cn.edu.hdu.Entity;
public class Factory {
private Integer factoryIndex;
private Integer factoryId;
private String systemName;
private String pc_ph;
private Integer modelNum;
private Integer modelId;
public Integer getFactoryIndex() {
return this.factoryIndex;
}
public void setFactoryIndex(Integer factoryIndex) {
this.factoryIndex = factoryIndex;
}
public Integer getFactoryId() {
return this.factoryId;
}
public void setFactoryId(Integer factoryId) {
this.factoryId = factoryId;
}
public String getSystemName() {
return this.systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
public String getPc_ph() {
return this.pc_ph;
}
public void setPc_ph(String pc_ph) {
this.pc_ph = pc_ph;
}
public Integer getModelNum() {
return this.modelNum;
}
public void setModelNum(Integer modelNum) {
this.modelNum = modelNum;
}
public Integer getModelId() {
return this.modelId;
}
public void setModelId(Integer modelId) {
this.modelId = modelId;
}
}