personalweb

archived
git clone git://git.wimdupont.com/personalweb.git
Log | Files | Refs | LICENSE

commit f64a66426059522874d46f6fa0aa4f4b04001688
parent 40c705b5d8b958a64eebef74d13b23e47ee03598
Author: Wim Dupont <wim@wimdupont.com>
Date:   Tue, 17 Jan 2023 21:11:47 +0100

migrate to webflux


Former-commit-id: 96adc174b4f3cc1e6e1a0c1133c49302bbc3bf12
Diffstat:
Mpom.xml | 2+-
Msrc/main/java/com/wimdupont/personalweb/api/AffirmationApi.java | 19+++++++++----------
Dsrc/main/java/com/wimdupont/personalweb/config/RestTemplateConfig.java | 16----------------
Msrc/main/java/com/wimdupont/personalweb/config/SecurityConfig.java | 12+++++++-----
Asrc/main/java/com/wimdupont/personalweb/config/WebClientConfig.java | 19+++++++++++++++++++
Msrc/main/java/com/wimdupont/personalweb/controller/IndexController.java | 6++----
Msrc/main/resources/templates/home.html | 4+---
7 files changed, 39 insertions(+), 39 deletions(-)

diff --git a/pom.xml b/pom.xml @@ -27,7 +27,7 @@ </dependency> <dependency> <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-web</artifactId> + <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> diff --git a/src/main/java/com/wimdupont/personalweb/api/AffirmationApi.java b/src/main/java/com/wimdupont/personalweb/api/AffirmationApi.java @@ -3,23 +3,22 @@ package com.wimdupont.personalweb.api; import com.wimdupont.personalweb.api.dto.affirmation.Affirmation; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -import java.util.Optional; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; @Component @RequiredArgsConstructor public class AffirmationApi { - private final RestTemplate restTemplate; + private final WebClient webClient; private static final String AFFIRMATION_URL = "https://www.affirmations.dev/"; - public Optional<Affirmation> getAffirmation(){ - try { - return Optional.ofNullable(restTemplate.getForObject(AFFIRMATION_URL, Affirmation.class)); - } catch (Exception e) { - return Optional.empty(); - } + public Mono<Affirmation> getAffirmation() { + return webClient.get() + .uri(AFFIRMATION_URL) + .retrieve() + .bodyToMono(Affirmation.class); } + } diff --git a/src/main/java/com/wimdupont/personalweb/config/RestTemplateConfig.java b/src/main/java/com/wimdupont/personalweb/config/RestTemplateConfig.java @@ -1,16 +0,0 @@ -package com.wimdupont.personalweb.config; - -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.client.RestTemplate; - -@Configuration -@SuppressWarnings("unused") -public class RestTemplateConfig { - - @Bean - public RestTemplate restTemplate(RestTemplateBuilder builder) { - return builder.build(); - } -} diff --git a/src/main/java/com/wimdupont/personalweb/config/SecurityConfig.java b/src/main/java/com/wimdupont/personalweb/config/SecurityConfig.java @@ -2,17 +2,19 @@ package com.wimdupont.personalweb.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity; +import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; +import org.springframework.security.config.web.server.ServerHttpSecurity; +import org.springframework.security.web.server.SecurityWebFilterChain; @Configuration -@EnableWebSecurity +@EnableWebFluxSecurity +@EnableReactiveMethodSecurity @SuppressWarnings("unused") public class SecurityConfig { @Bean - public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + public SecurityWebFilterChain filterChain(ServerHttpSecurity http) { return http.formLogin().disable().build(); } } diff --git a/src/main/java/com/wimdupont/personalweb/config/WebClientConfig.java b/src/main/java/com/wimdupont/personalweb/config/WebClientConfig.java @@ -0,0 +1,19 @@ +package com.wimdupont.personalweb.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.client.WebClient; + +@Configuration +@SuppressWarnings("unused") +public class WebClientConfig { + + @Bean + public WebClient webClient() { + return WebClient.builder() + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .build(); + } +} diff --git a/src/main/java/com/wimdupont/personalweb/controller/IndexController.java b/src/main/java/com/wimdupont/personalweb/controller/IndexController.java @@ -1,7 +1,6 @@ package com.wimdupont.personalweb.controller; import com.wimdupont.personalweb.api.AffirmationApi; -import com.wimdupont.personalweb.api.dto.affirmation.Affirmation; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -16,9 +15,8 @@ public class IndexController { @GetMapping("/") @SuppressWarnings("unused") public String home(Model model) { - model.addAttribute("affirmation", affirmationApi.getAffirmation() - .orElseGet(() -> new Affirmation("You're the best!"))); - + model.addAttribute("affirmation", affirmationApi.getAffirmation()); return "home"; } + } diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html @@ -10,9 +10,7 @@ <h1 class="home-title">Home</h1> <div th:insert="~{navigation}"/> </header> - <div th:if="${affirmation != null}"> - <p class="subheader" th:text="${affirmation.affirmation}"/> - </div> + <p class="subheader" th:text="${affirmation.affirmation}"/> <img title="The Way Of Kings" src="/images/TheWayOfKings.png" width="960"> <div class="source-credit"> Original art by Michael Whelan