2013-11-01 37 views
1

我在Java RMI中开发了一个简单的不可信系统,现在我必须将其更改为Web Services。我有一个问题,我的数据结构:WebService中的Hashtable更新java

Hashtable<String, ArrayList<Records>> recordsTable;

它不序列/正确更新我的对象。

我很笨,如何改变我的数据结构来克服这样的问题?

将帖子

为了简单起见,我说有这种数据结构:

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

我有一个购买()和显示()服务,其发布。最初我在店里有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

向我们显示teh codez –

+0

请张贴一些代码来重现问题。 'Records'是否实现了'java.io.Serializable'接口? – Bizmarck

+0

@SeanPatrickFloyd我添加了一个我的问题的简单代码 – newbieLinuxCpp

回答

0

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

+0

我尝试使用static关键字,仍然相同,不正确更新。 – newbieLinuxCpp

+0

发布其余的代码,然后(即相关的服务器代码) – Bizmarck