0

Just need guidance... From controller to service, variables are accessible. But not available when they are send to api side. (With one variable it's working perfectly fine) The variable values are coming after some calculations.

Service.js code...

function MySvcFunction(value1, value2) {
    console.log('value1 : ', value1);  //printing value1
    console.log('value2 : ', value2);  //printing value2
    return $http.post('http://' + api_url + value1, value2).then(
         function (response) {
              //manage success
         },
         function (error) {
              //manage error;
         });
}

Controller.js code...

$scope.someFunction = function(){
    console.log('value1 : ', value1);  //printing value1
    console.log('value2 : ', value2);  //printing value2
  MySvcController.MySvcFunction($scope.value1, $scope.value2).then(
  function (response) {
     //display on screen 
 });

And now api code in java...

Scenario-1 exception (with two @RequestBody)

@PostMapping(value = "/api_url")
public ResponseEntity<Object> MyFunction(@RequestBody Integer value1, @RequestBody Integer value2) {
    System.out.println("value1 : "+ value1);
    System.out.println("value2 : "+ value2);        
}
//Exception:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Stream closed; nested exception is java.io.IOException*/

Scenario-2 exception (with one @RequestBody)

@PostMapping(value = "/api_url")
public ResponseEntity<Object> MyFunction(@RequestBody Integer value1, Integer value2) {
    System.out.println("value1 : "+ value1);   //value1 : int val
    System.out.println("value2 : "+ value2);   //value2 : null
}
//Exception:
nested NullPointerException with root cause.
3
  • This might help you(stackoverflow.com/questions/12893566/…) Commented Aug 8, 2017 at 7:09
  • It's vague in sending parameters byhttp service. if it is query param so you should use in server side @RequestParam or if you pass object so use RequestBody and if you are using restfull so use @PathVariable Commented Aug 8, 2017 at 7:12
  • @Hadi, how to send two variable using @RequestBody? And I have posted an answer. Is it right ...? Commented Aug 9, 2017 at 6:20

1 Answer 1

1

I got it working not sure whether right or not?

Controller.js

$scope.someFunction = function(){
  var bothVar = {'value1': $scope.value1, 'value2': $scope.value2};
  MySvcController.MySvcFunction(bothVar).then(
    function (response) {
      //display on screen 
});

Service.js

function MySvcFunction(bothVar) {
 return $http.post('http://' + api_url + bothVar).then(
     function (response) {
          //manage success
     },
     function (error) {
          //manage error;
     });
}

API side java code

@PostMapping(value = "/api_url")
public ResponseEntity<Object> suggestBreakfast(@RequestBody Map bothVar){
    System.out.println("value1 is : "+ bothVar.get("value1"));
    System.out.println("value2 is : "+ bothVar.get("value2"));
}
// And i am getting those two values here successfully
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, It's OK. and may be better use HashMap<String,Object>,But It's better than use object for this. i.e create a class that has data member.

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.