WebService java中的Hashtable更新

时间:2013-11-01 13:22:09

标签: java web-services hashtable

我在Java RMI中开发了一个简单的不受信任的系统,现在我必须将其更改为Web服务。我的数据结构有问题:

Hashtable<String, ArrayList<Records>> recordsTable;

它没有正确序列化/更新我的对象。

我无法如何改变我的数据结构来克服这个问题?

[被修改]

为简单起见,请说我有这样的数据结构:

Hashtable<String, Integer> store = new Hashtable<String, Integer>();

我有一个已发布的buy()和display()服务。最初我在商店里有100个苹果,所以当我买()10个苹果时,它会打印出90个苹果的结果。 但是当我稍后调用显示器时,它会打印100个苹果。

所以有序列化问题我不知道如何解决这个问题。

public class StoreServer{

Hashtable<String, Integer> store= new Hashtable<String, Integer>();

public StoreServer()
{
    store.put("Coffee", 20);
    store.put("Apple", 100);
    store.put("Banana", 50);
    display();
}

public String buy(String item, int quantity)
{
    if(store.containsKey(item))
    {
        int oldQuantity = store.get(item);
        int newQuantity;
        if(oldQuantity-quantity>=0)
        {
            newQuantity= oldQuantity -quantity;
            store.put(item, newQuantity);
            return quantity+" "+item+" were successfully purchased!\n" +

                    ("1. Coffee: "+store.get("Coffee")+"\n")+
                    ("2. Apples: "+store.get("Apple")+"\n")+
                    ("3. Bananas: "+store.get("Banana")+"\n")+
                    ("---------------------------\n");
        }
        else
        {
            return "error with your purchase";
        }
    }
    else
    {
        return "error with your purchase";
    }
}


public void display()
{
    System.out.println("------Store Inventory-----");
    System.out.println("1. Coffee: "+store.get("Coffee"));
    System.out.println("2. Apples: "+store.get("Apple"));
    System.out.println("3. Bananas: "+store.get("Banana"));
    System.out.println("---------------------------");
}}

1 个答案:

答案 0 :(得分:0)

Web服务使用无状态协议。您需要将商店HashMap设置为静态。
此外,请确保它是ConcurrentHashMap,因为它可能同时被多个线程访问。