ARTICLE AD BOX
I've been passionate about Java for many years and learn new things for myself as a hobby. I'm trying to complete a project where I'm learning Java and Spring programming. For several days now, I've been trying to solve a problem I'm stuck on. I can't figure out where I went wrong or why it's not working.
I am currently trying to learn how WebClient(WebFlux) works in Java Spring.
I've created a REST API and a API Consumer that will interact with it via a website. However, I can't get the main page to work, which is supposed to display items added to the database.
Please help me, any suggestions would be appreciated!
I get the following ERROR message:
[SpringAppConsumer] [nio-8081-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: invalid URI scheme localhost] with root cause java.lang.IllegalArgumentException: invalid URI scheme localhost at java.net.http/jdk.internal.net.http.common.Utils.newIAE(Utils.java:286) ~[java.net.http:na] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ Request to GET localhost:/ [DefaultWebClient]Code from REST API
@RestController @RequiredArgsConstructor @RequestMapping("/products") public class ProductController { private final ProductService productService; @GetMapping("/") public List<ProductDto> findAllProducts(){ return productService.findAllProducts(); } @Service @RequiredArgsConstructor public class ProductService { private final ModelMapper modelMapper; private final ProductRepo productRepo; public List<ProductDto> findAllProducts() { Iterable<Product> products = productRepo.findAll(); return Arrays.asList(modelMapper.map(products, ProductDto[].class)); }Code from API consumer
@Controller @RequestMapping("/") public class ProductController { private final CartService cartService; @GetMapping public String showHome(Model model){ model.addAttribute("products", cartService.getAllProducts() ); return "home"; } @Service public class CartService { private final ProductService productService; public List<Product> getAllProducts() { return productService.getProducts(); } @Service public class ProductService { private final WebClient webClient = WebClient .builder() .baseUrl(Config.BASE_URL) .build(); public List<Product> getProducts() { return webClient .get() .uri("/") .retrieve() .bodyToFlux(Product.class) .collectList() .block(); } @Configuration public class Config { public static final String BASE_URL = "localhost:8080/products";HTLM
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Terra Silica</title> <link rel="stylesheet" type="text/css" href="/css/style.css"> </head> <body> <header th:replace="fragments/header :: ShopHeader"></header> <div class="main-content"> <div class="product" th:if="${products}"> <div class="product" th:each="product : ${products}"> <p class="item-name" th:text="${product.name}"></p> <img th:src="@{${product.imgURL}}"> <a th:href="@{'/add/' + ${product.id}}" class="add-btn">Add</a> </div> </div> </div> </body> </html>