2015-04-12 49 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註冊的,它應該工作。