ireport参数传递json_Json传递数据两种方式(json大全)
1.Json傳遞數據兩種方式(json大全)
----------------------------字符串
var list1 = ["number","name"];
var param={};
param["list1"] =list1;
param["test"] ="java";
var json=JSON.stringify(param);
$.ajax({
type:'POST',
url: CTX+ '雙擊查看原圖eckInvoice雙擊查看原圖st',
data: {
ids: json
},
dataType:"json",
async:false,
success: function(data){if(data.success) {
value= 'success';
$("#table").bootstrapTable('refresh');
}else{
value= 'fail';
}
showAlertFrame(value, data.message);
},
error: function (e) {
}
});
};
@PostMapping(value = "/list")
@ResponseBodypublic voidinvoiceDetail2(String ids) {
JSONObject jsonobject=JSONObject.parseObject(ids);
JSONArray jsonArray= JSONArray.parseArray(jsonobject.get("list1").toString());
List list = jsonArray.toJavaList(String.class);
}
@PostMapping(value= "/list2")
@ResponseBodypublic voidlist2(@RequestBody ScannerVo vo) {
List list =vo.getList1();//JSONArray jsonArray = JSONArray.parseArray(vo.getList1());//List list = jsonArray.toJavaList(String.class);
}
-------------------------------------對象
public class ScannerVo {
private List list1;
public List getList1() {
return list1;
}
public void setList1(List list1) {
this.list1 = list1;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
private String test;
}
var getInvoice = function (invoiceCode,invoiceNum) {
var list1 = ["number","name"];
var param = {};
param["list1"] = list1;
param["test"] ="java";
var json = JSON.stringify(param);
$.ajax({
type: 'POST',
url: CTX + '/checkInvoice/list2',
data: json,
dataType: "json",
async:false,
contentType: 'application/json',
success: function(data){
if(data.success) {
value = 'success';
$("#table").bootstrapTable('refresh');
} else {
value = 'fail';
}
showAlertFrame(value, data.message);
},
error: function (e) {
}
});
};
@PostMapping(value = "雙擊查看原圖st2")
@ResponseBodypublic voidlist2(@RequestBody ScannerVo vo) {
List list =vo.getList1();//JSONArray jsonArray = JSONArray.parseArray(vo.getList1());//List list = jsonArray.toJavaList(String.class);
}
springmvc接收json數據的4種方式
ajax我經常用到,傳的數據是json數據,json數據又有對象,數組。所有總結下springmvc獲取前端傳來的json數據方式:
1、以RequestParam接收
前端傳來的是json數據不多時:[id:id],可以直接用@RequestParam來獲取值
@AutowiredprivateAccomodationService accomodationService;
@RequestMapping(value= "/update")
@ResponseBodypublic String updateAttr(@RequestParam ("id") intid) {int res=accomodationService.deleteData(id);return "success";
}
2、以實體類方式接收
前端傳來的是一個json對象時:{【id,name】},可以用實體類直接進行自動綁定
@AutowiredprivateAccomodationService accomodationService;
@RequestMapping(value= "/add")
@ResponseBodypublicString addObj(@RequestBody Accomodation accomodation) {this.accomodationService.insert(accomodation);return "success";
}
!
3、以Map接收
前端傳來的是一個json對象時:{【id,name】},可以用Map來獲取
@AutowiredprivateAccomodationService accomodationService;
@RequestMapping(value= "/update")
@ResponseBodypublic String updateAttr(@RequestBody Mapmap) {if(map.containsKey("id"){
Integer id= Integer.parseInt(map.get("id"));
}if(map.containsKey("name"){
String objname= map.get("name").toString();
}//操作 ...
return "success";
}
4、以List接收
當前端傳來這樣一個json數組:[{id,name},{id,name},{id,name},...]時,用List接收
@AutowiredprivateAccomodationService accomodationService;
@RequestMapping(value= "/update")
@ResponseBodypublic String updateAttr(@RequestBody Listlist) {for(Accomodation accomodation:list){
System.out.println(accomodation.toString());
}return "success";
}
Springmvc接受json參數總結
關于springmvc的參數我覺得是個頭痛的問題,特別是在測試的時候,必須要正確的注解和正確的contype-type 后端才能被正確的請求到,否則可能報出400,415等等bad request。
1,最簡單的GET方法,參數在url里面,比如:
@RequestMapping(value = “/artists/{artistId}”, method = {RequestMethod.GET})
@PathVariable去得到url中的參數。
public Artist getArtistById(@PathVariable String artistId)
2,GET方法,參數接在url后面。
@RequestMapping(value = "/artists", method ={RequestMethod.GET})publicResponseVO getAllArtistName(
@RequestParam(name= "tagId", required = false) final String tagId)
訪問的時候/artists?tagId=1
@RequestParam相當于request.getParameter(“”)
3,POST方法,后端想得到一個自動注入的對象
@RequestMapping(value = "/addUser", method ={RequestMethod.POST})public void addUser(@RequestBody UserPO users){
這里要注意@RequestBody,它是用來處理前臺定義發來的數據Content-Type: 不是application/x-www-form-urlencoded編碼的內容,例如application/json, application/xml等;我們使用@RequestBody注解的時候,前臺的Content-Type必須要改為application/json,如果沒有更改,前臺會報錯415(Unsupported Media Type)。后臺日志就會報錯Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported
如果是表單提交 Contenttype 會是application/x-www-form-urlencoded,可以去掉@RequestBody注解
---------------------
這時聰明的spring會幫我按照變量的名字自動注入,但是這是很容易遇到status=400
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Feb 06 16:49:34 GMT+08:00 2018There was an unexpected error (type=Bad Request, status=400).Validation failed for object='user'. Error count: 11這是springboot的報錯,原因是bean中有不能注入的變量,因為類型的不一樣,一般是date和int的變量,所以在使用的時候要特別注意。
---------------------
如果前端使用的$.ajax來發請求,希望注入一個bean。這時又有坑了,代碼如下:
$.ajax({
headers: {
Accept:"application/json; charset=utf-8"},
method :'POST',
url:"http://localhost:8081/user/saveUser",
contentType:'application/json',
dataType:"json",
data: json,//async: false,//true:異步,false:同步//contentType: false,//processData: false,
success: function (data) {if(data.code == "000000"){
alert(data.desc);
window.location.href="http://localhost:8081/login.html";
}
},
error: function (err) {
alert("error");
}});---------------------
馬上就報錯了:
**error
:
“Bad Request”
exception
:
“org.springframework.http.converter.HttpMessageNotReadableException”
message
:
“JSON parse error: Unrecognized token ‘name’: was expecting ‘null’, ‘true’, ‘false’ or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘name’: was expecting ‘null’, ‘true’, ‘false’ or NaN? at [Source: java.io.PushbackInputStream@7fc056ba; line: 1, column: 6]”
path
:
“/user/saveUser”
status
:
400
timestamp
:
1518094430114**
這是看看發送的參數:
居然不是我拼裝好的json,
data: json,? 改成 data: JSON.stringify(json),后端接收json String,json只是個對象,所以解析不了!
---------------------
4,POST方法,需要得到一個List的類型
@RequestMapping(value = “/addUser”, method = {RequestMethod.POST})
public void addUser(@RequestBody List users){
---------------------
5,POST方法,后臺需要得到一個List類型。
@RequestMapping(value = "/getPlayURL", method ={RequestMethod.POST})
@ResponseBodypublic ListgetPlayUrlBySongIds(
@RequestParam(name= "songId",required = false) ListsongIdList) {---------------------
004-SpringMVC-如何接收各種參數(普通參數,對象,JSON, URL)
在交互的過程中,其中一個關鍵的節點就是獲取到客戶端發送過來的請求參數,本篇文章,我們來羅列下SpringMVC對于各種數據的獲取方式:說明:以下重點在講解如何獲取參數上,所以返回的數據不是重點1,普通方式,請求參數名跟Controller的方法參數一致1.1 創建Controller
---------------------
1.2 發送請求做測試(由于方法沒有限制請求方式,所以get和post均可)
2,當請求參數過多時,以對象的方式傳遞
2.1 創建一個類,包含多個參數(簡單不附帶圖了)
2.2 前臺傳遞參數的方式不變
2.3 后臺接收參數的方法
原因很簡單,是因為SpringMVC默認是沒有對象轉換成json的轉換器,所以需要手動添加jackson依賴。com.fasterxml.jackson.corejackson-databind2.8.8
---------------------
3,當請求參數名跟方法參數名不一致時,@RequestParam
4,當需要傳遞Json格式的數據是,@RequestBody
4.1 前臺傳遞的方式是json
5,通過URL的方式傳遞參數
總結
以上是生活随笔為你收集整理的ireport参数传递json_Json传递数据两种方式(json大全)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php知识点_PHP那些琐碎的知识点(整
- 下一篇: ppt表格无法调整单元格行高