☰
-
Php 95
-
Java 94
-
Javascript 37
-
Regex 18
-
Git 17
-
Security 16
-
Docker 07
-
Python 58
-
Machine Learning 14
-
Book Highlights
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 8
-
Quick start S
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
R
Q
Consume
The application will retrive quotes from a RESTful web service. When you request the service URL you receive a JSON document.
# https://quoters.apps.pcfone.io/api/random
{
type: "success",
value: {
id: 10,
quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
}
}
Start a new Spring Boot Web project.
package com.example.consumingrest;
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);
}
}
Create a domain class to contain the data that you need.
Any property not bound will be ignored.
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties (ignoreUnknown = true) // Look Here
public class Quote {
private String type;
private Value value;
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setValue(Value value) {
this.value = value;
}
public Value getValue() {
return value;
}
@Override
public String toString() {
return "Quote{type='" + type + "', value=" + value + "}";
}
}
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties (ignoreUnknown = true)
public class Value {
private Long id;
private String quote;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setQuote(String quote) {
this.quote = quote;
}
public String getQuote() {
return quote;
}
@Override
public String toString() {
return "Value{id='" + id + "', quote=" + quote + "}";
}
}
A RestTemplate will use the Jackson JSON library for incomming data.
A CommandLineRunner will run the RestTemplate on startup.
package com.example.consumingrest;
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);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) // on startup
throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"https://quoters.apps.pcfone.io/api/random", Quote.class
);
System.out.println(quote);
};
}
@GetMapping("/getquote")
public String quote() {
RestTemplate rt = new RestTemplate(); // on request
Quote quote = rt.getForObject(
"https://quoters.apps.pcfone.io/api/random", Quote.class
);
return quote.toString();
}
}
Build and test the application in console or browser.
./mvnw spring-boot:run
#console
2021-09-10 18:37:21.306 INFO 3273
Quote{type='success', value=Value{id='9', quote='Spring Boot solves ...'}
#browser
localhost:8080/getquote
Quote{type='success', value=Value{id='5', quote='Spring Boot solves ...'}
➥ Questions
Last update: 331 days ago