[英]Resteasy @FormParam null when passing params with AngularJS
我正在嘗試在前端使用AngularJS而在Resteasy中使用Rest API。 我的問題是,當使用AngularsJS發送params時,我的@FormParam總是為null。
這是我的JS:
$scope.addPerson = function(newFirstName, newLastName) {
$http({
method: 'POST',
url: "rest/persons/savePerson",
data: {firstName: 'newFirstName', lastName: 'newLastName'},
headers: {'Content-Type': 'application/json'}
})
.success(function(data) {
alert("DATA : " + data);
}).error(function(data, status, headers, config) {
alert("Could not save new person");
});
};
這是我的代碼服務器端:
@POST
@Path("/savePerson")
@Produces("application/json")
@Secured({ "ROLE_USER" })
public PersonBean savePerson(@FormParam("firstName") String firstName,
@FormParam("lastName") String lastName) {
if (firstName== null || lastName== null) {
return null;
}
PersonBean person = personDao.savePerson(firstName,
lastName);
return person ;
}
任何幫助表示贊賞。
您將字段發布為JSON,而不是表單編碼數據。
headers: {'Content-Type': 'application/json'}
但你不能只改變這個標題。 這些值需要進行Form編碼。 請參閱此頁面,了解如何從Angular發布表單數據。
http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
雖然,您可能會發現最好堅持使用JSON並讓您的框架將這些字段映射到Bean。
我沒有使用Resteasy,但我認為它應該像......一樣簡單
@POST
@Path("/savePerson")
@Consumes("application/json")
@Produces("application/json")
@Secured({ "ROLE_USER" })
public PersonBean savePerson(SavePersonBean savePersonBean) {
PersonBean person = personDao.savePerson(savePersonBean.getFirstName(),
savePersonBean.getLastName());
return person;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.