Java 如何将大量参数从Angular js传递到rest服务

Java 如何将大量参数从Angular js传递到rest服务,java,angularjs,rest,Java,Angularjs,Rest,我正在尝试使用**@queryParam**从angular JS$http服务到rest服务获取参数。我需要获取很多参数(下面以3为例,但我需要使用其中的12-15个,我需要将它们传递到java端),因此使用@QueryParam获取所有参数会使代码看起来非常糟糕。我使用的是GET 我如何优化它 例如我正在做的事情- 角Js代码- $http({ url: someUrl, method: "GET", params: {filter1: $scope.filter1

我正在尝试使用**
@queryParam**
从angular JS
$http
服务到rest服务获取参数。我需要获取很多参数(下面以3为例,但我需要使用其中的12-15个,我需要将它们传递到java端),因此使用@QueryParam获取所有参数会使代码看起来非常糟糕。我使用的是
GET

我如何优化它

例如我正在做的事情-

角Js代码-

$http({
    url: someUrl, 
    method: "GET",
    params: {filter1: $scope.filter1,
filter2:$scope.filter2,
filter3:$scope.filter3
 });
Java端-

@path("/getAllData")
@GET
@Produces({..}
public response getAllData(@QueryParam("filter1") final String filter1,
                           @QueryParam("filter2") final String filter2,
                           @QueryParam("filter3") final String filter3){
}
另外,想知道当我构建URL而不是params对象,并使用
@PathParam

$http.get('rest/test/getAllData/?filter1='$scope.filter1 + 
           '&filter2='$scope.filter2 + '&filter3='$scope.filter3 + 
           '&filter4='$scope.filter4)

我可以通过在@QueryParam中单独传递来完成。当我们需要大量参数时,我正在寻找优化的代码。

创建一个包含所有必需参数的POJO

在角度上,这样做

var obj = {};
obj.filter1 =  $scope.filter1;
obj.filter2 =  $scope.filter2;
obj.filter3 =  $scope.filter3;


$http({
    url: someUrl, 
    method: "GET",
    params: obj
});
您可以接受rest中的所有参数,如下所示-

@path("/getAllData")
@GET
@Produces({..}
public response getAllData(MyPojo obj){
  //String filter1 = obj.filter1;
}

您可以通过两种方式完成:

1)
org.json.simple.JSONObject

2) Bean或POJO类

AngularJS控制器:

var URL = appURL+'/adm/addCollProcess.do';
var json = {"col_pro_id":$scope.col_pro_id, "col_code": $scope.col_code, "exam_type_ids": $scope.exam_types.toString().replace("[","").replace("]",""), 
    "created_by" : "admin", "file_path" : $scope.file_path, "website" : $scope.website, "facebook" : $scope.facebook};

// using JSONObject
$http.post(URL, json).then(function(response){
    if(response.data){
       // Code
    }
});

// using Bean Class
 $http.post(URL, JSON.stringify(json)).then(function(response){
    if(response.data){
       // Code
    }
});
// using JSONObject
@RequestMapping(value="/addCollProcess.do", method=RequestMethod.POST)
public boolean addCollProcess(@RequestBody JSONObject json){
    // Code
}

// using Bean Class:
@RequestMapping(value="/addCollProcess.do", method=RequestMethod.POST)
public @ResponseBody boolean addCollProcess(@RequestBody AdmissionProcessBean processBean) {
    // Code
}
Java控制器:

var URL = appURL+'/adm/addCollProcess.do';
var json = {"col_pro_id":$scope.col_pro_id, "col_code": $scope.col_code, "exam_type_ids": $scope.exam_types.toString().replace("[","").replace("]",""), 
    "created_by" : "admin", "file_path" : $scope.file_path, "website" : $scope.website, "facebook" : $scope.facebook};

// using JSONObject
$http.post(URL, json).then(function(response){
    if(response.data){
       // Code
    }
});

// using Bean Class
 $http.post(URL, JSON.stringify(json)).then(function(response){
    if(response.data){
       // Code
    }
});
// using JSONObject
@RequestMapping(value="/addCollProcess.do", method=RequestMethod.POST)
public boolean addCollProcess(@RequestBody JSONObject json){
    // Code
}

// using Bean Class:
@RequestMapping(value="/addCollProcess.do", method=RequestMethod.POST)
public @ResponseBody boolean addCollProcess(@RequestBody AdmissionProcessBean processBean) {
    // Code
}
可能重复