AngularJS GET Spring MVC Through Service Controller

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 GET Spring MVC Through Service Controller

  • 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:

AngularJS GET Spring MVC Through Service Controller

  • Please refer AngularJS official tutorial for more details here 

download Download Project: SpringMVCAngularJSService

2 thoughts on “AngularJS GET Spring MVC Through Service Controller”

Leave a Reply

Your email address will not be published. Required fields are marked *