0

I am trying to fetch parameters from angular JS $http service to rest service using **@queryParam** . 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.

How can I optimize this ?

Example what I am doing -

Angular Js code -

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

Java side -

@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

$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 . I am looking for optimized code when we a large number of parameters.

1

2 Answers 2

0

Create a POJO with all the required parameters.

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it in 2 ways:

1) org.json.simple.JSONObject.

2) Bean or POJO Class.

AngularJS Controller:

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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.