Ich versuche gerade zu lernen, wie WebClient (WebFlux) in Java Spring funktioniert.
Ich habe eine REST-API und einen API-Consumer erstellt, der über eine Website damit interagiert. Allerdings kann ich die Hauptseite, auf der zur Datenbank hinzugefügte Elemente angezeigt werden sollen, nicht zum Laufen bringen.
Bitte helfen Sie mir, ich bin für Vorschläge dankbar!
Ich erhalte die folgende FEHLERmeldung:
Code: Select all
[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: Select all
@RestController
@RequiredArgsConstructor
@RequestMapping("/products")
public class ProductController {
private final ProductService productService;
@GetMapping("/")
public List
findAllProducts(){
return productService.findAllProducts();
}
Code: Select all
@Service
@RequiredArgsConstructor
public class ProductService {
private final ModelMapper modelMapper;
private final ProductRepo productRepo;
public List findAllProducts() {
Iterable products = productRepo.findAll();
return Arrays.asList(modelMapper.map(products, ProductDto[].class));
}
Code: Select all
@Controller
@RequestMapping("/")
public class ProductController {
private final CartService cartService;
@GetMapping
public String showHome(Model model){
model.addAttribute("products", cartService.getAllProducts() );
return "home";
}
Code: Select all
@Service
public class CartService {
private final ProductService productService;
public List
getAllProducts() {
return productService.getProducts();
}
Code: Select all
@Service
public class ProductService {
private final WebClient webClient = WebClient
.builder()
.baseUrl(Config.BASE_URL)
.build();
public List getProducts() {
return webClient
.get()
.uri("/")
.retrieve()
.bodyToFlux(Product.class)
.collectList()
.block();
}
Code: Select all
@Configuration
public class Config {
public static final String BASE_URL = "localhost:8080/products";
Code: Select all
Terra Silica
[url=@{]Add[/url]
Mobile version