Simple Tutorial on working with Java Spring and AngularJS together


In this post, we have created a Simple Tutorial explaining integration of Spring with Angular. For better understanding, we would create a Sample Application that fetches Roll number and name of Student from user and displays it.

  1. In the Eclipse IDE, Create a Maven project named "Tutorial".
  2. Create a package under src/main/java folder named "com.example" to store all our java source files.
  3. Folder structure

  4. Integrate AngularJS with Spring RESTful API Web Application

Append the dependencies in POM.xml

A Project Object Model or POM is an XML file that contains information about the project and the configurations used by Maven to build the project.

Project dependencies are specified in POM.xml, these dependencies are the files you'll need to execute the project.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation=""> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>Tutorial</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Spring Angular WebApp</name> <url></url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.3</version> </dependency> </dependencies> <build> <finalName>Tutorial</finalName> </build> </project>

Under com.example package, create This class file is a RESTful Webservice which directs the user to app.jsp file. It also stores information of Student and returns it in a JSON format.

package com.example; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @RestController public class AppController { @RequestMapping(value="/app") public ModelAndView app() { return new ModelAndView("app"); } @RequestMapping(value="/studentdetails",method=RequestMethod.GET,produces="application/json") public StudentDetails studentdetails() { StudentDetails StudentDetails = new StudentDetails(); StudentDetails.setRoll(21); StudentDetails.setName("Krishna"); return StudentDetails; } }

Under com.example package, create another java class file, This file contains getter and setter methods which lets us set data to object variable or get data from object variable.

package com.example; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class StudentDetails { @XmlAttribute private int roll; @XmlAttribute private String name; public StudentDetails() { super(); } public StudentDetails(int roll, String name) { super(); this.roll = roll; = name; } public int getRoll() { return roll; } public void setRoll(int roll) { this.roll = roll; } public String getName() { return name; } public void setName(String name) { = name; } }


Create a jsp file named "app.jsp" in /WEB-INF/Jsp folder.

<html ng-app>
<script src=""></script>
function App($scope, $http) {
$scope.getStudentDetails = function()
success(function(data) {
$scope.user = data;
<div ng-controller="App">
<h2>Java Spring and Angular WebApplication</h2>
<button ng-click="getStudentDetails()">View</button>
<p>Roll No. : {{user.roll}}</p>
<p>Name : {{}}</p>


When the user clicks "View" button, getStudentDetails() method is called. It uses $http service which calls REST service and fetch studentDetails in JSON format which is rendered on the screen.
Create index.jsp file in WEB-INF folder.
<%@ taglib uri="" prefix="c" %> <% response . sendRedirect("app");%> <html> <body> <h2>This won't rendered as this file just redirects to app</h2> </body> </html>

This jsp file redirects user to /app and the controller redirects the user to app.jsp which contains our main information.


Edit web.xml file under WEB-INF directory, and add SpringConfiguration file. This configuration file is loaded when the Servlet is initialized.

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "" > <web-app> <display-name>Spring AngularJS Tutorial</display-name> <servlet> <servlet-name>SpringAngular</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SpringAngular</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>


Create SpringAngular-servlet.xml file under WEB-INF directory. This lets Spring container search for all the annotations under "com.example" package.

<beans xmlns="" xmlns:xsi="" xmlns:context="" xmlns:mvc="" xsi:schemaLocation=""> <context:component-scan base-package="com.example" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/Jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>


Creating a Simple Web App With Java 8, Spring, JSP and Angular JS

Spring and Angular JS simple Tutorial

Clear Any Java Interview by Reading our Ebook Once.

Can you clear Java Interview?



Hot Deals ends in

Technical Quizzes Specially For You:

Search Tags

    Java Spring and Angular integration project

    Example project for java spring and angular