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 Consuming Rest
http://localhost:8080/api/random http://localhost:9090/getquote
REST Service
Run rest_service in a separate terminal on port 9090.
cd rest_service/
mvn spring-boot:run
http://localhost:9090 # Welcome! Use /quote/{id}
The client app will retrive quotes from the REST web service.
http://localhost:9090/getquote/1 # {"id":1,"content":"Spring Boot makes ...
http://localhost:9090/getquote/2 # {"id":2,"content":"With Boot you ...
Client App
Start a new Spring Boot Web project on port 8080
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Mapping
Application class with /request_quote mapping.
/**
* Consuming REST App
*
* Add mapping (/getquote) from a separate service on 8080 port
* Use a domain class (Quote) to contain the data that you need.
* Any property not bound will be ignored.
*
* RestTemplate will use the Jackson JSON library for incomming data.
* CommandLineRunner will run the RestTemplate on startup.
*/
package com.minte9.consuming_rest;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@RestController
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@GetMapping("/")
public String home() {
return "REST Api Client <br><br>Use /request_quote";
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@GetMapping("/request_quote")
public Object quote() {
RestTemplate rt = new RestTemplate(); // Browser
Object quote = rt.getForObject(
"http://localhost:9090/quote/1", Object.class
);
return quote; // {"id":1,"content":"Spring Boot makes ...
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) // Console
throws Exception {
return args -> {
Object quote = restTemplate.getForObject(
"http://localhost:9090/quote/2", Object.class
);
System.out.println(quote); // {"id":2,"content":"With Boot ...
};
}
}
Test requests
Build and run the application in console or browser.
cd consuming_rest/
mvn spring-boot:run
# Browser
http://localhost:9090/getquote # {"id":1,"content":"Spring Boot makes ...
# Console
java -jar target/consuming_rest-0.0.1.jar # {"id":2,"content":"With Boot ...
➥ Questions