Skip to main content

Posts

Showing posts with the label Spring Boot Examples

Spring Boot-Schedule a task with SpringBoot-Download Source Code

In this article, You’ll learn how to schedule tasks in Spring Boot using @Scheduled annotation.To schedule job in spring boot application to run periodically, spring boot provides @EnableScheduling and @Scheduled annotations.
Add @EnableScheduling to Spring Boot Application class
Add @EnableScheduling annotation to your spring boot application class. @EnableScheduling is a Spring Context module annotation. It internally imports the SchedulingConfiguration via the @Import(SchedulingConfiguration.class) instruction

@SpringBootApplication @EnableScheduling public class KnowledgefactoryScheduler {
public static void main(String[] args) { SpringApplication.run(KnowledgefactoryScheduler.class, args); } }

1. Scheduling a Task with Fixed Rate
You can schedule a method to be executed at a fixed interval by using fixedRate parameter in the @Scheduled annotation. In the following example, The annotated method will be executed every 3 seconds. // Scheduling a Task with Fixed Rate
@Scheduled(fixedRate = 3000) p…

WebSocket + Spring boot web application example with source code download

In this article, we will show you how to develop a Simple Spring Boot web application with WebSocket
Why WebSocket?
We already know traditional HTTP requests are unidirectional and heavy.But WebSocket is bi-directional.The initial connection is using HTTP, then this connection gets upgraded to a socket-based connection. This single connection is then used for all future communication.The WebSocket message data exchange is much lighter compared to HTTP
Following technologies stack being used:

Spring Boot 2.1.4.RELEASESpring 5.1.6.RELEASE JDK 1.8Eclipse OxygenBootstrapThymeleafWebSocket
User Interface

Project Structure
Pom.xml[maven]
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
 …

Configuring Tomcat Connection pool and Hikari Connection pool in Spring Boot

One key component of spring boot starter dependencies is spring-boot-starter-data-JPA. This allows us to use JPA and work with production databases by using some popular JDBC connection pooling implementations, such as HikariCP and Tomcat JDBC Connection Pool.
Tomcat Connection Pooling
Spring Boot will look for HikariCP on the classpath and use it by default when present
To configure a Tomcat JDBC connection pool instead of the default HikariCP, we'll exclude HikariCP from the spring-boot-starter-data-jpa dependency and add the tomcat-jdbc Maven dependency to our pom.xml:
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
   <exclusions>
    <exclusion>
     <groupId>com.zaxxer</groupId>
     <artifactId>HikariCP</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat</groupId>
  …

DataTable-Pagination example with Spring boot,jQuery and ajax free download

This article shows you DataTable-Pagination example with Spring boot, jQuery and ajax. 

Following technologies stack being used:
Spring Boot 2.1.1.RELEASESpring 5.1.3.RELEASE Maven 3DatatablejQueryFree Markerjavascriptajax Project Structure
View(index.ftl) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Knf|Pagination-Datatables</title> <!-- Tell the browser to be responsive to screen width --> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="../../css/jquery.dataTables.min.css"> <body>  <section class="content">   <div class="container-fluid">    <table id="example" class="display" style="width: 100%">     <thead>      <tr>       <th>First name</th>       <th>La…

Spring Boot upload/download file example using Ajax and jQuery-download source code

This article shows you how to upload file/download files in the Spring Boot web application, using Ajax and jquery. Following technologies stack being used:

Spring Boot 2.1.1.RELEASESpring 5.1.3.RELEASE Maven 3JDK 1.8Eclipse OxygenjQueryBootstrapFree Markerjavascriptajax
User Interface

Project Structure

Home Controller
private static final String path = "D:/files/";
 @Autowired
 UploadDownloadService service;
  @PostMapping("/uploadFile")
 public ResponseEntity<List<String>> fileUpload(@RequestParam("file") MultipartFile file) throws Exception {
  return new ResponseEntity<>(service.uploadFile(file), HttpStatus.OK);
  }
  @RequestMapping(path = "/download", method = RequestMethod.GET)
 public ResponseEntity<Resource> download(@RequestParam String param) throws IOException {
   File file = new File(path + param);
   Path path = Paths.get(file.getAbsolutePath());
  ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path))…