[英]How to post a json array to java servlet with jquery
我目前正在開發spring mvc
應用程序,我需要發布JSON array
。
當我訪問request.getParameter("paramValue")
來獲取param attibute時,它返回一個null
值,
這是我的前端代碼:
$.ajax(url, {
async: true,
type: 'post',
contentType: 'application/json',
data: JSON.stringify({
"test":"test value"
})
}).done(function (response) {
console.log(data);
}).fail(function (xhr) {
console.log("request failed");
console.log(xhr);
});
這是我的服務器端代碼:
@RequestMapping(value = "/Products", method = RequestMethod.POST)
public void saveProducts(HttpServletRequest req, HttpServletResponse res) throws Exception {
System.out.println(req.getContentType());
System.out.println(req.getContentLength());
System.out.println(req.getContextPath());
System.out.println(req.getParameterValues("test"));
System.out.println(req.getMethod());
StringBuilder buffer = new StringBuilder();
BufferedReader reader = req.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String data = buffer.toString();
System.out.println(data);
System.out.println(req.getParameter("test"));
}
輸出是:
application/json
22
null
POST
{"test" : "Test DAta"}
null
我無法弄清楚發生了什么,請幫助我。
在你的ajax函數中刪除這一行
contentType: 'application/json',
並替換此行
data: JSON.stringify({
"test":"test value"
})
同
data: {
"test":"test value"
}
而你也可以使用
req.getParameter("test")
代替
req.getParameterValues("test")
你可以使用這個:
var data ={id: 1, name :'test'}
$.ajax(url, {
async: true,
type: 'post',
contentType: 'application/json',
data: data
}).done(function (response) {
console.log(data);
}).fail(function (xhr) {
console.log("request failed");
console.log(xhr);
});
並在服務器端創建一個pojo:
public class Product{
private long id;
private String name;
// getters and setters
添加庫傑克遜。
在您的控制器中添加此方法:
@RequestMapping(value = "/Products", method = RequestMethod.POST)
public RepsoneEntity<? >saveProducts(@requestBody Product pr){
LOG.debug(pr.toString());
return new reRepsoneEntity<Product>(pr,HttpStatus.ACCEPTED);
}
我最后修改了幾個注釋並更改了服務器端方法的返回類型,
@RequestMapping(value = "/Products", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
}).done(function (response) { console.log(response); }).fail(function (xhr) { console.log("request failed"); console.log(xhr); });
我使用org.json來訪問解析為文本的json對象,使用gson來處理POJO
現在它的工作原理:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.