AngularJs POST-GET Nested Objects to SpringBoot server

In this tutorial, JavaSampleApproach will guide you to implement a web application AngularJs POST-GET Nested Objects to SpringBoot server.

Related posts:
How to integrate Http Angularjs with Spring MVC | Spring Boot
Angular 4 – Send Nested Object to Spring Boot Server
AngularJs Http Put Form Data to Remote RestAPIs | SpringBoot
JQuery uses Bootstrap Table to display data from SpringBoot RestAPI
AngularJs POST-GET LIST Objects to SpringBoot server
Html5 DateTime + AngularJs + SpringBoot @JsonFormat example

I. Technologies

– Java 1.8
– Maven 3.6.1
– Spring Tool Suite – Version 3.8.1.RELEASE
– AngularJs
– Bootstrap
– Spring Boot – 1.5.7.RELEASE

II. Practice

In the tutorial, we build a SpringBoot project as below:

angularjs post-get nested objects to springboot server - project structure

Step to do:
– Create Spring Boot project
– Create data models
– Create Web Controller to provide web views
– Create RestController for POST & GET requests
– Create an index.html view
– Create AngularJs POST & GET controller
– Run & Check results

1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, add project info, then press Next for needed dependencies:
– For Template Engines, choose Thymeleaf.
– For Web MVC, choose Web->Web.

AngularJs POST-GET Nested Objects to SpringBoot - add dependencies

Open pom.xml file and check needed dependencies:


	org.springframework.boot
	spring-boot-starter-thymeleaf


	org.springframework.boot
	spring-boot-starter-web

2. Create data models

– Create Address model:

package com.javasampleapproach.angularjs.model;

public class Address {
	 
	private String street;
	private String postcode;
	
	public Address(){}
	
	public Address(String street, String postcode){
		this.street = street;
		this.postcode = postcode;
	}
 
	public String getStreet() {
		return street;
	}
 
	public void setStreet(String street) {
		this.street = street;
	}
 
	public String getPostcode() {
		return postcode;
	}
 
	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}
 
	@Override
	public String toString() {
		return "Address {street:" + street + ", postcode:" + postcode + "}";
	}
}

– Then create Customer model that includes Address model:

package com.javasampleapproach.jqueryajax.model;
 
public class Customer {
	 
	private String name;
	private Integer age;
	private Address address;
	
	public Customer(){}
	
	public Customer(String name, Integer age, Address address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public Integer getAge() {
		return age;
	}
 
	public void setAge(Integer age) {
		this.age = age;
	}
 
	public Address getAddress() {
		return address;
	}
 
	public void setAddress(Address address) {
		this.address = address;
	}
 
	@Override
	public String toString() {
		return "Customer [name=" + name + ", age=" + age + ", address=" + address + "]";
	}
 
}
3. Create Web Controller to provide web views
package com.javasampleapproach.angularjs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class WebController {
	@GetMapping(value="/")
    public String homepage(){
        return "index";
    }
}
4. Create RestController for POST & GET requests
package com.javasampleapproach.angularjs.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javasampleapproach.angularjs.model.Customer;

@RestController
@RequestMapping("/api/customer")
public class RestWebController {
	
	List cust = new ArrayList();
	
	@GetMapping(value = "/all")
	public List getResource(){
			return cust;
	}
	
	@PostMapping(value="/save")
	public Customer postCustomer(@RequestBody Customer customer){
		cust.add(customer);
		return customer;
	}
}
5. Create an index.html view




	  Spring Boot - AngularJs Example
	  
	  %MINIFYHTML1712d07f238306888f93f6e0291dce4016%%MINIFYHTML1712d07f238306888f93f6e0291dce4017%
	  



Http AngularJs - GET/POST Nested Objects

Post Successfully!
-->: {{"{name: " + postCust.name + ", age: " + postCust.age + ", street: " + postCust.address.street + ", postcode: " + postCust.address.postcode + "}"}}

  • {{"{name: " + cust.name + ", age: " + cust.age + ", street: " + cust.address.street + ", postcode: " + cust.address.postcode}}
%MINIFYHTML1712d07f238306888f93f6e0291dce4018%
6. Create AngularJs POST & GET controller
var app = angular.module('app', []);

// #######################
// POST CONTROLLER
//#######################

app.controller('postcontroller', function($scope, $http, $location) {
	
	// be used to decide for showing PostResults   
	$scope.postDivAvailable = false;
	// be used for decide for showing GetResults 
	$scope.getDivAvailable = false;
	
	$scope.submitForm = function(){
		// post URL
		var url = $location.absUrl() + "api/customer/save";
		
		// prepare headers for posting
		var config = {
                headers : {
                	'Content-Type': 'application/json',
                    'Accept': 'application/json'
                }
        }
		
		// prepare data for post messages
		var data = {
            name: $scope.name,
            age: $scope.age,
            address: {
            	street: $scope.street,
            	postcode: $scope.postcode
            }
        };
		
		// do posting
		$http.post(url, data, config).then(function (response) {
			$scope.postDivAvailable = true;
			$scope.postCust =  response.data;
		}, function error(response) {
			$scope.postResultMessage = "Error Status: " +  response.statusText;
		});
		
		// reset data of form after posting
		$scope.name = '';
		$scope.age = '';
		$scope.street = '';
		$scope.postcode = '';
	}
});

//#######################
//GET CONTROLLER
//#######################

app.controller('getcontroller', function($scope, $http, $location) {
	
	$scope.getfunction = function(){
		// get URL
		var url = $location.absUrl() + "api/customer/all";
		
		// do getting
		$http.get(url).then(function (response) {
			$scope.getDivAvailable = true;
			$scope.response = response.data;
		}, function error(response) {
			$scope.postResultMessage = "Error Status: " +  response.statusText;
		});
	}
});
7. Run & Check results

Build & Run project with Spring Boot mode.
–> Result:

angularjs post-get nested objects to springboot server - results

III. Sourcecode

SpringAngularJsNestedObject



By grokonez | October 20, 2017.


Related Posts


Got Something To Say:

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

*