Spring MVC - UnknownFormatConversionException

时间:2016-01-26 19:19:25

标签: spring spring-mvc

我遇到UknownFormatConversionException问题。有人能帮助我吗? 我正在使用Spring Web Flow在我的应用程序中编写客户订单。

这是问题:

java.util.UnknownFormatConversionException: Conversion = ')'
    at java.util.Formatter.checkText(Formatter.java:2579)
    at java.util.Formatter.parse(Formatter.java:2565)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.util.Formatter.format(Formatter.java:2455)
    at java.lang.String.format(String.java:2940)
    at com.packt.webstore.domain.repository.impl.InMemoryCartRepository.delete(InMemoryCartRepository.java:40)
    at com.packt.webstore.service.impl.CartServiceImpl.delete(CartServiceImpl.java:34)
    at com.packt.webstore.service.impl.OrderServiceImpl.saveOrder(OrderServiceImpl.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

InMemoryCartRepository

@Repository
public class InMemoryCartRepository implements CartRepository{
    private Map<String, Cart> listOfCarts = new HashMap<String,Cart>();
    public void delete(String cartId) {
        if(!listOfCarts.keySet().contains(cartId)) {
            throw new IllegalArgumentException(String.format("Nie można usunąć koszyka. Koszyk o wskazanym id (%) nie istnieje." , cartId));
        }
        listOfCarts.remove(cartId);
    }
}

CartServiceImpl:

@Service
public class CartServiceImpl implements CartService{

    @Autowired
    private CartRepository cartRepository;

public void delete(String cartId) {
        cartRepository.delete(cartId);

    }
}

OrderServiceImpl:

@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private CartService cartService;
    public Long saveOrder(Order order) {
        Long orderId = orderRepository.saveOrder(order);
        cartService.delete(order.getCart().getCartId());
        return orderId;
    }

}

1 个答案:

答案 0 :(得分:1)

您的String.format会抛出此异常:格式无效,(%)中缺少's':它应为(%s)

throw new IllegalArgumentException(String.format("Nie można usunąć koszyka. Koszyk o wskazanym id (%s) nie istnieje." , cartId));