首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自GET的org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500内部服务器错误

来自GET的org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500内部服务器错误
EN

Stack Overflow用户
提问于 2022-10-07 09:49:44
回答 1查看 203关注 0票数 0

路径[]抛出异常[请求处理失败;嵌套异常为org.springframework.http.converter.HttpMessageConversionException:类型定义错误:简单类型,类org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$RepositoryAnnotationTransactionAttributeSource;嵌套异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException:,没有为类org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$RepositoryAnnotationTransactionAttributeSource找到序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS) (通过引用链:具有根本原因的java.util.ArrayList->com.sun.proxy.$Proxy98"advisors"->org.springframework.aop.support.DefaultPointcutAdvisor3->org.springframework.aop.support.DefaultPointcutAdvisor"advice"->org.springframework.transaction.interceptor.TransactionInterceptor"transactionAttributeSource")] )

order_service _microservice错误:

:路径[]抛出的异常请求处理失败的servlet http://localhost:9091/api/inventory/?skucode=order_10&skucode=order_20&skucode=order_30**的Servlet.service();嵌套的异常是来自GET http://localhost:9091/api/inventory/?skucode=order_10&skucode=order_20&skucode=order_30**的带有根本原因的Servlet.service 500内部服务器错误

org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500内部服务器错误来自org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:239) ~spring的GET http://localhost:9091/api/inventory/?skucode=order_10&skucode=order_20&skucode=order_30 -5.3.23.jar:5.3.23抑制: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:错误已在以下站点观察到:来自GET http://localhost:9091/api/inventory/?skucode=order_10&skucode=order_20&skucode=order_30 DefaultWebClient原始堆栈跟踪的*__checkpoint⇢500 : at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientRespons

INVENTORY_microservice:

控制器类:

代码语言:javascript
复制
package com.Inventory_microservice.Inventory_microservice.Controller;

import com.Inventory_microservice.Inventory_microservice.Repository.InventoryRepository;
import com.Inventory_microservice.Inventory_microservice.Service.InventoryService;
import com.Inventory_microservice.Inventory_microservice.model.Inventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<InventoryRepository> isInStock(@RequestParam List<String> skucode ) {
        return inventoryService.isInStock(skucode);

    }
}

服务类:

代码语言:javascript
复制
package com.Inventory_microservice.Inventory_microservice.Service;

import com.Inventory_microservice.Inventory_microservice.Repository.InventoryRepository;
import com.Inventory_microservice.Inventory_microservice.dto.InventoryResponse;
import com.Inventory_microservice.Inventory_microservice.model.Inventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InventoryService {

    @Autowired
    private InventoryRepository inventoryRepository;

    @Transactional
    public List<InventoryRepository> isInStock(List<String> Skucode)
    {

        return inventoryRepository.findBySkucodeIn(Skucode).stream().map(inventory -> checkstock(inventory)).collect(Collectors.toList());
    }

    private InventoryRepository checkstock(Inventory inventory) {
        InventoryResponse inventoryResponse=new InventoryResponse();
        inventoryResponse.setSkucode(inventory.getSkucode());
        inventoryResponse.setInstock(inventory.getQuantity()>0);
        return inventoryRepository;
    }

}

ORDER_MICROSERVICE:

com.OrderService.order_service_microsevice.Controller;控制器类

代码语言:javascript
复制
import com.OrderService.order_service_microsevice.Service.OrderService;
import com.OrderService.order_service_microsevice.dto.RequestOrder;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public String createOrder(@RequestBody @NotNull RequestOrder requestOrder){


        System.out.println("calling Order controller class");
        orderService.placeOrder(requestOrder);
        return  "Order placed Successfully";
    }

}

服务舱:

代码语言:javascript
复制
package com.OrderService.order_service_microsevice.Service;

import com.OrderService.order_service_microsevice.Repository.OrderRepository;
import com.OrderService.order_service_microsevice.dto.InventoryResponse;
import com.OrderService.order_service_microsevice.dto.OrderListItemsDto;
import com.OrderService.order_service_microsevice.dto.RequestOrder;
import com.OrderService.order_service_microsevice.model.Order;
import com.OrderService.order_service_microsevice.model.OrderItemList;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class OrderService {

    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private WebClient webClient;

    public void placeOrder(@NotNull RequestOrder requestOrder) {

        Order order = new Order();
        order.setOrderNumber(UUID.randomUUID().toString());

        List<OrderItemList> orderItemLists =requestOrder.getOrderListItemsDtos()
                .stream()
                .map(this::maptodto)
                .collect(Collectors.toList());
        order.setOrderItemList(orderItemLists);

        List<String> skucodes=order.getOrderItemList()
                .stream()
                .map(orderItemList -> orderItemList.getSkucode())
                .collect(Collectors.toList());


        //here we need to call inventory microservice to get the response
        InventoryResponse[] inventoryResponsesArray=webClient.get()
                .uri("http://localhost:9091/api/inventory/",
                        uriBuilder -> uriBuilder.queryParam("skucode",skucodes)
                                .build())
                .retrieve()
                .bodyToMono(InventoryResponse[].class)
                .block();

        boolean allproductsinstock= Arrays.stream(inventoryResponsesArray).allMatch(InventoryResponse::isInstock);

        if(allproductsinstock){
            orderRepository.save(order);
        }
        else{
            throw new IllegalArgumentException("Product is not available in stock");
        }

    }

    private @NotNull OrderItemList maptodto(@NotNull OrderListItemsDto orderListItemsDto) {
        OrderItemList orderItemList = new OrderItemList();
        orderItemList.setSkucode(orderListItemsDto.getSkucode());
        orderItemList.setQuantity(orderListItemsDto.getQuantity());
        orderItemList.setPrice(orderListItemsDto.getPrice());

        return orderItemList;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-10-08 06:27:08

问题似乎出现在你的“GetMapping”和“PostMapping”中

您正在尝试返回一个InventoryRepository列表,这是不可能的,您应该返回一个实体列表。

代码语言:javascript
复制
@Autowired
    private InventoryService inventoryService;
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<InventoryRepository> isInStock(@RequestParam List<String> skucode ) {
        return inventoryService.isInStock(skucode);

    }

相反,你应该做的是:

代码语言:javascript
复制
 @Autowired
    private InventoryService inventoryService;
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<Inventory> isInStock(@RequestParam List<String> skucode ) {
        return inventoryService.isInStock(skucode);

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73985345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档