Java
/
Spring Boot
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
S
R
Q
Java Spring Boot Ajax
@RestController @GetMapping("/start") $.ajax({url: "http://localhost:8080/start"});
Ajax App
Create a new Spring Web project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

/**
* Spring Boot Web App (Ajax)
*/
package com.minte9.ajax;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Client
The index.html loads into the user's browser the client that consume REST service.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
function crono() {
setTimeout(function() {
$.ajax({
url: "http://localhost:8080/getcurrtime"
}).then(function(data){
$("#crono").html(data.time);
});
crono();
}, 1000);
}
crono();
});
function start() {
$.ajax({
url: "http://localhost:8080/start"
});
}
function stop() {
$.ajax({
url: "http://localhost:8080/stop"
});
}
</script>
</head>
<body>
Get current time (with Ajax):
<a href='javascript: start();'>Start</a> |
<a href='javascript: stop();'>Stop</a>
<div id='crono'></div>
</body>
</html>
Service
The REST controller displays the response as JSON.
package com.minte9.ajax;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CronoController {
private Boolean start = true;
private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
private String lastTime = dateFormat.format(new Date());
@GetMapping("/getcurrtime")
public Crono crono() {
if (start) {
String time = dateFormat.format(new Date());
lastTime = time;
}
return new Crono(lastTime);
}
@GetMapping("/start")
public void start() {
this.start = true;
}
@GetMapping("/stop")
public void stop() {
this.start = false;
}
}
/**
* Data representation class (to return as JSON)
*/
class Crono {
private final String time;
public Crono(String time) {
this.time = time;
}
public String getTime() {
return time;
}
}
Run
Test and run the application.
mvn spring-boot:run
http://localhost:8080/
// Get current time (with Ajax): Start | Stop
// 13:21:55
➥ Questions