我有一個應用程序Cordova和AngularJS。
使用Angular向我的後端應用程序發送一個值(Spring REST)。我使用的方法是$ http.post。
問題
當我嘗試將數據發送到我的服務器春天不會在我的實體設置的值。由於這個原因,我無法保存我的新數據。
角碼
我AngularJS代碼如下:
httpService.createInBackend = function (url, data, callback, errorCallback) {
http.post(url, data)
.then(function (success) {
callback(success);
}, function (error) {
errorCallback(error.data);
});
};
我用下面的參數:
網址:http://<location>:<port>/<application>/<web_socket_url>
數據:
data: {
{
"incidentTypeId": 5,
"priorityId": 1,
"creationDate": 1449676871234,
"userId": 1,
"latitude": <some valid location>,
"longitude": <some valid location>
}
},
timeout: 4000
}
我用「數據」而不是「PARAMS」,因爲我想通過身體發送數據。我得到這個與PUT函數一起工作,這與這個函數沒什麼不同。
我的REST控制器
@RequestMapping(value = "/employee/new", method = RequestMethod.POST)
public NewEmployee createIncident(@RequestBody NewEmployee employee) {
return employee;
}
我NewEmployee模式
private int employeeId;
private int priorityId;
private String information;
private Date creationDate;
private int userId;
private float longitude;
private float latitude;
角結果
當使用控制檯,我得到這個方法如下結果:
我送:
{
"employeeId":5,
"priorityId":1,
"creationDate":1449677250732,
"userId":1,
"information": "hello world!",
"latitude":<some valid location>,
"longitude":<some valid location>
}
我收到:
{
employeeId: 0
priorityId: 0
creationDate: null
userId: 0
information: null
latitude: 0
longitude: 0
}
郵遞員
我想同樣的事情郵差(谷歌chrome插件)和這樣我的代碼工作。由於這一點,我認爲這是我的AngularJS代碼的問題。
我已經試過
我曾嘗試使用以下AngularJS調用嘗試:
$http({
method: 'POST',
url: url,
data: data,
timeout: 4000
}).then(function (success) {
callback(success);
}, function (error) {
errorCallback(error);
});
然而,這並沒有改變結果。仍然只有空值。
有沒有人知道我在做什麼錯?
什麼類型的內容類型是Spring REST期待的請求? –
默認它必須是json – theodor
我在我的Spring中(通過Maven)有Jackson,而且我使用@enableWebMvc,所以我可以自動反序列化Json。 –