0
我無法得到這個東西的工作。除了我嘗試發佈實際對象時,該項目完全可以工作。我可以發佈單個字符串就好了。發佈對象spring restcontroller?
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<h1>post test</h1>
<button id="postButton">post</button>
<script>
var myObject = {
name : "Johnny",
field : "something"
};
$(function() {
$("#postButton").click(function() {
$.post("posting", JSON.stringify(myObject), function(data, status) {
alert(status);
});
});
});
</script>
</body>
</html>
控制器:
package app.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import model.MyObject;
@Controller
public class MainController {
@RequestMapping(value = "/posting", method = RequestMethod.POST)
@ResponseBody
public void posting(@RequestBody MyObject myObject) {
System.out.println(myObject.getName());
}
}
myObject的:
package model;
public class MyObject {
private String name;
private String field;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
任何幫助將不勝感激。我在沒有JSON.stringify的情況下嘗試了它,它也不起作用。