Angular JS post Jersey 对象为空
Angular JS post object to Jersey is null
我正在尝试使用 angular js 和 jersey post 我的表单数据到我的休息服务。但是,在其余服务中收到的 bean 为空。以下是 POST 请求的 angular 代码。
(function() {
var dataUrls = {
addSurveyUrl : '/CompanySurveyPortal/Company/Surveys/addSurvey'
}
angular.module('addNewSurvey', []).service('addSurveyDataService',
function($http) {
this.addNewSurvey = function(survey)
{
return $http.post(dataUrls.addSurveyUrl, {"surveyId": survey.surveyId, "title": survey.question, "choice1": survey.firstOption, "choice2": survey.secondOption ,"status": 'Open'});
};
})
.controller('AddNewSurveyController', function($scope, $log, responseDataService, $stateParams, $state, $filter, $rootScope, addSurveyDataService){
this.survey = {};
this.addSurvey = function(){
addSurveyDataService.addNewSurvey(this.survey);
};
function init(){
}
init();
})
})();
表单中的数据已成功填充到 $http.post 语句中。然而,每次使用请求的方法中的 bean 都是空的。
@POST
@Path("addSurvey")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_JSON)
public void addNewSurvey(@BeanParam Survey survey,
@Context HttpServletResponse servletResponse) throws IOException,
SQLException {
SurveyDao surveyDao = new SurveyDao();
surveyDao.addSurvey(survey);
}
我看到的主要问题是 @BeanParam
的不正确使用。这应该只用于你想将不同的 @XxxParam
注释参数组合成一个 POJO 的情况,比如
public class Survey {
@FormParam("id")
public String id;
@FormParam("title")
public String title;
}
那么 @BeanParam
就可以了,因为数据实际上是在 application/x-www-form-urlencoded
中输入的。但是您的请求实际上是以 JSON 的形式传入的,这意味着请求没有被转换为 Survey
如果您只是删除 @BeanParam
,并且您注册了 JSON provider,它应该可以工作。
我正在尝试使用 angular js 和 jersey post 我的表单数据到我的休息服务。但是,在其余服务中收到的 bean 为空。以下是 POST 请求的 angular 代码。
(function() {
var dataUrls = {
addSurveyUrl : '/CompanySurveyPortal/Company/Surveys/addSurvey'
}
angular.module('addNewSurvey', []).service('addSurveyDataService',
function($http) {
this.addNewSurvey = function(survey)
{
return $http.post(dataUrls.addSurveyUrl, {"surveyId": survey.surveyId, "title": survey.question, "choice1": survey.firstOption, "choice2": survey.secondOption ,"status": 'Open'});
};
})
.controller('AddNewSurveyController', function($scope, $log, responseDataService, $stateParams, $state, $filter, $rootScope, addSurveyDataService){
this.survey = {};
this.addSurvey = function(){
addSurveyDataService.addNewSurvey(this.survey);
};
function init(){
}
init();
})
})();
表单中的数据已成功填充到 $http.post 语句中。然而,每次使用请求的方法中的 bean 都是空的。
@POST
@Path("addSurvey")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_JSON)
public void addNewSurvey(@BeanParam Survey survey,
@Context HttpServletResponse servletResponse) throws IOException,
SQLException {
SurveyDao surveyDao = new SurveyDao();
surveyDao.addSurvey(survey);
}
我看到的主要问题是 @BeanParam
的不正确使用。这应该只用于你想将不同的 @XxxParam
注释参数组合成一个 POJO 的情况,比如
public class Survey {
@FormParam("id")
public String id;
@FormParam("title")
public String title;
}
那么 @BeanParam
就可以了,因为数据实际上是在 application/x-www-form-urlencoded
中输入的。但是您的请求实际上是以 JSON 的形式传入的,这意味着请求没有被转换为 Survey
如果您只是删除 @BeanParam
,并且您注册了 JSON provider,它应该可以工作。