Я пытаюсь завершить проект по изучению программирования на Java и Spring. Я пытаюсь решить проблему, с которой застрял, и не могу понять, где я ошибся.
В настоящее время я пытаюсь узнать, как WebClient (WebFlux) работает в Java Spring.
Я создал REST API и API-потребитель, который будет взаимодействовать с ним через веб-сайт. Однако я не могу заставить работать главную страницу, которая должна отображать элементы, добавленные в базу данных.
Что делать дальше?
Я получаю следующее сообщение ОШИБКА:
[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]
Код из REST API
@RestController
@RequiredArgsConstructor
@RequestMapping("/products")
public class ProductController {
private final ProductService productService;
@GetMapping("/")
public List
findAllProducts(){
return productService.findAllProducts();
}
@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));
}
Код от потребителя API
@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
getAllProducts() {
return productService.getProducts();
}
@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();
}
@Configuration
public class Config {
public static final String BASE_URL = "localhost:8080/products";
HTLM
Terra Silica
Add
Подробнее здесь: https://stackoverflow.com/questions/798 ... texception