简体   繁体   English

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

[英]How to pass large number of parameters from Angular js to rest service

I am trying to fetch parameters from angular JS $http service to rest service using ** @queryParam** . 我正在尝试使用** @queryParam**从有角JS $http服务获取参数到其余服务。 I need to fetch lot of parameters(below have shown for 3 as an example ,but I need to use around 12-15 of them which I need to pass to the java side) ,so fetching all with @QueryParam makes the code look pretty bad .I am using GET . 我需要获取很多参数(下面以3为例进行说明,但是我需要使用其中的12-15个参数,我需要传递给Java端),因此使用@QueryParam获取所有参数会使代码看起来很漂亮不好。我正在使用GET

How can I optimize this ? 我该如何优化呢?

Example what I am doing - 示例我在做什么-

Angular Js code - Angular Js代码-

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

Java side - Java端-

@path("/getAllData")
@GET
@Produces({..}
public response getAllData(@QueryParam("filter1") final String filter1,
                           @QueryParam("filter2") final String filter2,
                           @QueryParam("filter3") final String filter3){
}

Also ,wanted to know the optimization in case when I am building URL instead of params object, and picking the same with @PathParam 另外,想知道优化情况,以防我构建URL而不是params对象,并使用@PathParam

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

I am able to do it by passing individually in @QueryParam . 我可以通过在@QueryParam中单独传递来做到这一点。 I am looking for optimized code when we a large number of parameters. 当我们有大量参数时,我正在寻找优化的代码。

Create a POJO with all the required parameters. 使用所有必需的参数创建一个POJO。

In angular, do this 在角度,这样做

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


$http({
    url: someUrl, 
    method: "GET",
    params: obj
});

You can accept all the parameters in you rest like this - 您可以像这样接受休息中的所有参数-

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

You can do it in 2 ways: 您可以通过2种方式来做到这一点:

1) org.json.simple.JSONObject . 1) org.json.simple.JSONObject

2) Bean or POJO Class. 2)Bean或POJO类。

AngularJS Controller: 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
    }
});

Java Controller: Java控制器:

// 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在url中为逗号分隔参数传递休息服务的get方法 - How to pass comma separated parameters in a url for the get method of rest service 如何将输入参数传递给来自Rest Client的Jboss BRMS规则 - How to pass input parameters to a Jboss BRMS Rule from Rest Client 如何从 REST web 服务中的 curl 获取参数值 - how to get parameters' value from curl in REST web service in java 如何将参数传递给码头服务 - how to pass parameters to jetty service 如何将用户定义的对象从angularjs传递到REST服务 - How to pass user defined objects to REST service from angularjs 如何将参数从prototypejs客户端传递到REST Web服务 - How to pass parameter from prototypejs client to rest web service 如何将授权令牌从Web应用传递到Rest服务 - How to pass authorization token from webapp to rest service 怪异行为:Angular Js - Spring Rest 服务 - Weird Behavior: Angular Js - Spring Rest Service 如何将可变数量的参数从JSP页面传递到Java控制器 - How to pass variable number of parameters from jsp page to the java controller 如何在REST Web服务中处理具有多个参数的HTTP请求+限制数量范围 - How to process HTTP request with multiple parameters in REST web service + limit the number range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM