角JS对象后泽西岛为null对象、泽西、JS、null

2023-09-13 03:33:51 作者:亂世基佬。

我想我的表单数据发布到采用了棱角分明的js和球衣我的休息服务。然而,在其余的服务收到的bean是空。以下是我的角度code的POST请求。

 (函数(){VAR dataUrls = {    addSurveyUrl:'/ CompanySurveyPortal /公司/调查/ addSurvey}angular.module('addNewSurvey',[])。服务('addSurveyDataService',        功能($ HTTP){            this.addNewSurvey =功能(调查)            {                返回$ http.post(dataUrls.addSurveyUrl,{surveyId:survey.surveyId,头衔:survey.question,choice1:survey.firstOption,选择2:survey.secondOption,身份:打开 });            };        })        .controller('AddNewSurveyController',函数($范围,$日志,responseDataService,$ stateParams,$状态,$过滤器,$ rootScope,addSurveyDataService){            this.survey = {};            this.addSurvey =功能(){                addSurveyDataService.addNewSurvey(this.survey);            };            功能的init(){            }            在里面();        })})(); 

从表单中的数据被成功地填充在$ http.post声明。然而,在以消耗请求的方法的bean是空各一次。

  @POST@Path(addSurvey)@Produces(MediaType.TEXT_HTML)@Consumes(MediaType.APPLICATION_JSON)公共无效addNewSurvey(@BeanParam调查调查,        @Context HttpServletResponse的ServletResponse的)抛出IOException异常,        {的SQLException    SurveyDao surveyDao =新SurveyDao();    surveyDao.addSurvey(调查);} 

解决方案

我看到的主要问题,是不正确的使用 @BeanParam 的。这应该仅用于要不同的 @XxxParam 注释PARAMS结合成一个POJO,像

案例

 公共类调查{    @FormParam(ID)    公共字符串ID;    @FormParam(题)    公共字符串称号;} 
任由之 英法两不理,魅力有泽西

然后 @BeanParam 将工作,给出的数据实际上是在未来应用程序/ x-WWW的形式urlen codeD 。但是你的要求实际上是在即将到来的JSON,这意味着该请求不被转换为调查

如果你只是删除 @BeanParam ,和你有一个的 JSON提供商注册,它应该工作。

I am trying to post my form data to my rest service using angular js and jersey. However the bean received at the rest service is null. Following is my angular code for the POST request.

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

})();

The data from the form is successfully populated in the $http.post statement. The bean at the method to consume the request is however null each time.

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

}

解决方案

The main problem I see, is the incorrect use of @BeanParam. This should only be used for cases where you want to combine different @XxxParam annotated params into a POJO, like

public class Survey {
    @FormParam("id")
    public String id;
    @FormParam("title")
    public String title;
}

Then @BeanParam would work, given the data is actually coming in application/x-www-form-urlencoded. But your request is actually coming in as JSON, which means that the request is not being converted to the Survey

If you simply remove the @BeanParam, and you have a JSON provider registered, it should work.