2015-04-12 53 views
2

我想发表我的表格数据到我的休息服务使用角js和泽西岛。然而,其余服务收到的bean是空的。以下是我的POST请求的角码。泽西岛的角JS对象后null

(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); 

} 

回答

2

我看到的主要问题是错误使用@BeanParam。这应该仅用于要不同@XxxParam注释PARAMS结合成一个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注册的,它应该工作。