hibernate 获取Servlet的Servlet.service()错误[dispatcherServlet] java.lang.NullPointerException]

kcwpcxri  于 2023-10-23  发布在  Java
关注(0)|答案(1)|浏览(160)

我正在尝试创建一个订购服务。但是,当使用URL {http://localhost:8092/api/order}进行发布请求时,有效负载数据为:

{
    "OrderLineItemsDto":[
        {
            "skuCode":"i_Phone_13",
            "price":1200,
            "quantity":1
        }
    ]
}

这是一个错误:Servlet.service()用于servlet [dispatcherServlet]在路径[]的上下文中抛出异常[Request processing failed;嵌套异常是java.lang.NullPointerException],并带有根本原因。和空指针异常在这一点上在下面的服务类

List<OrderLineItems> orderLineItems = orderRequest.getOrderLineItemsDtoList()
                .stream() //Here it is giving the null pointer exception
                .map(this::mapToDto)
                .collect(Collectors.toList());
Entities
@Entity
@Table(name = "t_orders")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    private String orderNumber;
    @OneToMany(cascade = CascadeType.ALL)
    private List<OrderLineItems> orderLineItemsList;
}
@Getter
@Setter
@Entity
@Table(name = "t_order_line_items")
@AllArgsConstructor
@NoArgsConstructor
public class OrderLineItems {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    private String skuCode;
    private BigDecimal price;
    private Integer quantity;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class OrderLineItemsDto {
    private String id;
    private String skuCode;
    private BigDecimal price;
    private Integer quantity;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class OrderRequest {
    private List<OrderLineItemsDto> orderLineItemsDtoList;
}
Service Class
@Service
public class OrderServiceImpl implements OrderService {
    private final OrderRepository orderRepository;
    @Autowired
    public OrderServiceImpl(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    @Override
    public void placeOrder(OrderRequest orderRequest) {
        Order order = new Order();
        order.setOrderNumber(UUID.randomUUID().toString());

        List<OrderLineItems> orderLineItems = orderRequest.getOrderLineItemsDtoList()
                .stream() ***At This line it is giving null pointer exception***
                .map(this::mapToDto)
                .collect(Collectors.toList());

        order.setOrderLineItemsList(orderLineItems);
        orderRepository.save(order);
    }    private OrderLineItems mapToDto(OrderLineItemsDto orderLineItemsDto) {
        OrderLineItems orderLineItems = new OrderLineItems();
        orderLineItems.setPrice(orderLineItemsDto.getPrice());
        orderLineItems.setQuantity(orderLineItemsDto.getQuantity());
        orderLineItems.setSkuCode(orderLineItemsDto.getSkuCode());

        return orderLineItems;
    }
}
6mzjoqzu

6mzjoqzu1#

您的有效载荷与您的代码不匹配,它应该是:

{
    "orderLineItemsDtoList": [
        {
            "skuCode": "i_Phone_13",
            "price": 1200,
            "quantity": 1
        }
    ]
}

相关问题