java.lang.IllegalArgumentException:找不到产品

时间:2016-08-17 10:36:36

标签: java spring

嗨,在学习春天的时候,我陷入了以下代码,不知道这段代码有什么问题

@Repositiry

 @Repository
    public class ProductRepositiryImpl implements ProductRepositiry {

        private List<Product> listOfAllProducts = null;

        public ProductRepositiryImpl() {
            listOfAllProducts = new LinkedList<>();

            Product iphone = new Product("A1232", "iPhone", new BigDecimal(500));
            iphone.setDescription("iPhone 6s with 5.2' FHD Screen");
            iphone.setManfactuer("Apple");
            iphone.setCategory("Smart Phone");
            iphone.setUnitsInStock(699);

            listOfAllProducts.add(iphone);
        }

        @Override
        public List<Product> getAllProducts() {
            return listOfAllProducts;
        }

        @Override
        public Product getProductsById(String productId) {
            Product productById = null;
            for (Product product : listOfAllProducts) {
                if (product != null && product.getProductId() != null && product.getProductId().equals(productId)) {
                    productById = product;
                    break;
                }

                if (productById == null)
                    throw new IllegalArgumentException("no product found " + productId);

            }
            return productById;

        }

    }


**Service**



@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
ProductRepositiry productRepositiry;

@Override
public void processOrder(String productId, long count) {
    System.out.println("inside Process Order");

    Product getProductByid = productRepositiry.getProductsById(productId);
    System.out.println("get product by id");
    System.out.println(getProductByid);
    if (getProductByid.getUintsInOrder() < getProductByid.getUnitsInStock()) {
        throw new IllegalArgumentException("OUT OF STOCK" + getProductByid.getUintsInOrder());
    } 
        getProductByid.setUintsInOrder(getProductByid.getUintsInOrder() - count);
}
**@controller**



@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/order/A1234/2")
    public String processOrder() {
        System.out.println("process order");
        orderService.processOrder("A1232", 2);
        return "redirect:/product";
    }
}

StackTrack

  

SEVERE:上下文中servlet [dispatcherServlet]的Servlet.service()   与路径[/ web_Store]抛出异常[请求处理失败;   嵌套异常是java.lang.IllegalArgumentException:没有产品   发现A1233]的根本原因java.lang.IllegalArgumentException:no   产品发现A1233 at   com.webstore.repoImpl.ProductRepositiryImpl.getProductsById(ProductRepositiryImpl.java:58)     在   com.webstore.serviceImpl.OrderServiceImpl.processOrder(OrderServiceImpl.java:20)     在   com.webstore.controller.OrderController.processOrder(OrderController.java:18)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)

在类ProductRepositiryImpl中,每当我尝试执行方法getProductsById(String productId)并且在调试时我得到productById = product;持有有效值,那么它应该退出循环,为什么它仍然在IllegalArgumentException 1}} 任何帮助都会被暗示

最好的问候

1 个答案:

答案 0 :(得分:1)

public ProductRepositiryImpl() {
            listOfAllProducts = new LinkedList<>();

            Product iphone = new Product("A1232", "iPhone", new BigDecimal(500));
            iphone.setDescription("iPhone 6s with 5.2' FHD Screen");
            iphone.setManfactuer("Apple");
            iphone.setCategory("Smart Phone");
            iphone.setUnitsInStock(699);

            listOfAllProducts.add(iphone);
    }

您正在使用 ID A1232

添加产品
@RequestMapping("/order/A1234/2")
    public String processOrder() {
        System.out.println("process order");
        orderService.processOrder("A1233", 2);
        return "redirect:/product";
    }

您正尝试使用 ID A1233

获取产品

编辑1:

正如@Florian上述评论所述。移动检查{for循环外的productById == null

public Product getProductsById(String productId) {
            Product productById = null;
            for (Product product : listOfAllProducts) {
                if (product != null && product.getProductId() != null && product.getProductId().equals(productId)) {
                    productById = product;
                    break;
                }              
            }
            if (productById == null)
                    throw new IllegalArgumentException("no product found " + productId);

           return productById;

}