JQuery uses Bootstrap Table to display data from SpringBoot RestAPI

In this tutorial, JavaSampleApproach will show you how to implement a web application which uses JQuery & Bootstrap Table to display data from SpringBoot RestAPI.

Related posts:
Jquery Ajax POST-GET Nested Objects to SpringBoot server
Bootstrap Filter Table with JQuery and SpringBoot RestAPI
JQuery Ajax Http Delete remove data from SpringBoot RestAPI
Jquery JsonP request to cross domain | Springboot RestAPI & BootStrap

I. Technologies

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

II. Practice

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

JQuery uses Bootstrap Table to display data from SpringBoot RestAPI - project structure

Step to do:
– Create Spring Boot project
– Create data models
– Create Web Controller to provide web view
– Create RestController for GET request
– Create an index.html view
– Create JQuery Ajax GET request
– Run & Check results

1. Create Spring Boot project

Using Spring Tool Suite to create a Spring Starter Project. Add project info, then press Next for needed dependencies:

JQuery uses Bootstrap Table to display data from SpringBoot RestAPI - dependency

After creating project successfully, 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.jqueryboostraptable.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 + "}";
	}
}

– Create Customer model that includes Address model:

package com.javasampleapproach.jqueryboostraptable.model;

public class Customer {
	private Long id;
	private String name;
	private Integer age;
	private Address address;
	
	public Customer(){}
	
	public Customer(Long id, String name, Integer age, Address address){
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
 
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	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 view
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 GET request
package com.javasampleapproach.jqueryboostraptable.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

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

import com.javasampleapproach.jqueryboostraptable.model.Address;
import com.javasampleapproach.jqueryboostraptable.model.Customer;

@RestController
@RequestMapping("/api/customer")
public class RestAPIs {
	
	Map custStores = new HashMap();
	
	@PostConstruct
    public void initIt() throws Exception {
        custStores.put(Long.valueOf(1), new Customer(new Long(1), "Jack", 25, new Address("NANTERRE CT", "77471")));
        custStores.put(Long.valueOf(2), new Customer(new Long(2), "Mary", 37, new Address("W NORMA ST", "77009")));
        custStores.put(Long.valueOf(3), new Customer(new Long(3), "Peter", 18, new Address("S NUGENT AVE", "77571")));
        custStores.put(Long.valueOf(4), new Customer(new Long(4), "Amos", 23, new Address("E NAVAHO TRL", "77449")));
        custStores.put(Long.valueOf(5), new Customer(new Long(5), "Craig", 45, new Address("AVE N", "77587")));
    }
	 
	@GetMapping(value = "/all")
	public List getResource() {
		
		List custList = custStores.entrySet().stream()
		        .map(entry -> entry.getValue())
		        .collect(Collectors.toList());
		
		return custList;
	}
}
5. Create an index.html view

 


      Spring Boot - DELETE-UPDATE AJAX Example
      
      %MINIFYHTML8b06d96882aede3231910f0aaff2dcee14%%MINIFYHTML8b06d96882aede3231910f0aaff2dcee15%%MINIFYHTML8b06d96882aede3231910f0aaff2dcee16%

 

Customer Table

Id Name Age Street Postcode
%MINIFYHTML8b06d96882aede3231910f0aaff2dcee17%

.table-bordered class is used to add borders on all sides of the table and cells.
.table-hover class is used to add a hover effect on table rows.
.table-responsive class is used to create a responsive table. The table will scroll horizontally on small devices (under 768px).

6. Create JQuery Ajax GET request
$(document).ready(function() {
	
	ajaxGet();
	
	// DO GET
	function ajaxGet(){
		$.ajax({
			type : "GET",
			url : window.location + "api/customer/all",
			success: function(result){
				$.each(result, function(i, customer){
					
					var customerRow = '' +
										'' + customer.id + '' +
										'' + customer.name.toUpperCase() + '' +
										'' + customer.age + '' +
										'' + customer.address.street + '' +
										'' + customer.address.postcode + '' +
									  '';
					
					$('#customerTable tbody').append(customerRow);
					
		        });
				
				$( "#customerTable tbody tr:odd" ).addClass("info");
				$( "#customerTable tbody tr:even" ).addClass("success");
			},
			error : function(e) {
				alert("ERROR: ", e);
				console.log("ERROR: ", e);
			}
		});	
	}
})
7. Run & Check results

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

JQuery uses Bootstrap Table to display data from SpringBoot RestAPI - result

JQuery uses Bootstrap Table to display data from SpringBoot RestAPI - response return (2)

III. Sourcecode

SpringBootJQueryBootstrapTable



By grokonez | November 2, 2017.


Related Posts


Got Something To Say:

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

*