bszyd
bszyd

Reputation: 27

How to connect angularjs $http.get with spring's @GetMapping

    $scope.getProjectDetails = function(theId){
       $http.get('${pageContext.request.contextPath}/api/project', {params: {theId}})
          .then(function(response){
             $scope.project = response.data;
          });
       }

this one generetes:

GET http://localhost:8080/editor-application/api/project?theId=1 404

and spring rest wants:

http://localhost:8080/editor-application/api/project/1 

controller:

@GetMapping("/project/{theId}")    
public Project getProject(@PathVariable int theId) {

return editorService.getProject(theId);
}

how to make them speak with each other?

Upvotes: 0

Views: 53

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16785

You are trying to send a variable as a get parameter but in the back-end you are expecting it as a path variable. There can be two solutions to handle this:

  1. Modify the Spring controller to accept the request parameter:
@GetMapping("/project")    
public Project getProject(@RequestParam int theId) {
    ...
}

There are no modifications needed for the AngularJS code.

  1. Modify the $http request to send the variable as a path variable.
$http.get('${pageContext.request.contextPath}/api/project/' + theId)
    .then(function(response){
        ...
    });

In this case the controller does not need any modifications.

Upvotes: 2

Related Questions