AngularJS GET Spring MVC Through Service Controller
In this demo we will make call to Spring MVC controller using AngularJS http get method. In AngularJS page we will use service to send request and process response inside controller:
- Maven project structure:
- AngularJS index.html page:
<!DOCTYPE html> <html data-ng-app="serviceModule"> <head> <meta charset="ISO-8859-1"> <title>AngularJS GET Spring MVC Through Service Controller</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> <script type="text/javascript"> var serviceModule = angular.module('serviceModule', []); serviceModule.controller('LoadFileController', function($scope, sampleService) { $scope.variable = "AngularJS GET Spring MVC Through Service Controller"; sampleService.getDataSeriveExample().then(function(response){ $scope.environment = response.data; console.log($scope.environment); }); }); serviceModule.service('sampleService', function($http) { this.getDataSeriveExample = function() { var promise = $http.get('getService').success(function(data, status, headers, config) { return data; }); return promise; }; }); </script> </head> <body data-ng-controller="LoadFileController"> <div> <h2>{{variable}}</h2> <p>Response: {{environment}}</p> </div> </body> </html>
- SpringMVCController.java:
package com.javahonk.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class SpringMVCController { @RequestMapping(value = "/getService") public @ResponseBody String getService() { return "AngularJS http get call successfully processed!"; } }
- Output:
- Please refer AngularJS official tutorial for more details here
Download Project: SpringMVCAngularJSService
Hi JavaHonk
I executed above program but i got the error.
ie) http://localhost:9090/SpringMVCAngularJS/getService 404 (Not Found)
how i solve it. Please give the solution.
Advance Thanks.
hi Java Honk,
Its working fine. Thank you very much