【问题标题】:Java web service seemingly not storing variable?Java Web 服务似乎不存储变量?
【发布时间】:2015-03-11 16:29:50
【问题描述】:

编辑:问题是双重的,第一个字典应该是静态的,而且我使用 .contains() 我应该使用 .containsKey()

我正在尝试设置一个简单的 java 客户端和服务器,这就是我所拥有的,我似乎无法发现我这样做的方式有什么问题,但是每当我运行代码时,我都会得到输出:

Result = Added

Result = This word is not in the dictionary, please use the add function.

这告诉我,当我添加一个单词时,服务器没有存储对地图所做的更改,我在这里缺少一些非常简单的东西吗?

如果需要,我可以添加更多信息。

这是我的客户端代码:

public class Client {
 @WebServiceRef(wsdlLocation = 
        "http://localhost:8080/P1Server/ServerService?wsdl")

public static void main(String[] args) { 
try { 
    package1.ServerService service = new package1.ServerService(); 
    package1.Server port = service.getServerPort(); 

    String result = port.addWord("Test", "This is a test."); 
    System.out.println("Result = " + result); 

    result = port.getDefiniton("Test");
    System.out.println("Result = " + result); 
}catch(Exception ex)
{ 
    System.out.println("Gone Wrong"); 
}

这是我的相关服务器代码:

@WebService
public class Server {

private **static**ConcurrentHashMap<String,String> dictionary;    

public Server() {
    this.dictionary = new ConcurrentHashMap<>();
}

@WebMethod
public String addWord(String word, String definition){
    if(dictionary.contains(word.toLowerCase())){
        return "This word is already in the dictionary, "
                + "please use the update function.";
    }else{
        dictionary.put(word.toLowerCase(), definition);
        return "Added";
    }
}
@WebMethod
public String getDefiniton(String word){
    if(dictionary.contains(word.toLowerCase())){
        return dictionary.get(word);

    }else{
        return "This word is not in the dictionary, "
                + "please use the add function.";
    }
}

【问题讨论】:

    标签: java server concurrenthashmap


    【解决方案1】:

    您的问题与网络服务有关。 问题在于你的逻辑

    修改你的方法如下:

    public String addWord(String word, String definition) {
            if (dictionary.containsKey(word.toLowerCase())) {
                return "This word is already in the dictionary, "
                        + "please use the update function.";
            } else {
                dictionary.put(word.toLowerCase(), definition);
                return "Added";
            }
        }
    
        public String getDefiniton(String word) {
            if (dictionary.containsKey(word.toLowerCase())) {
                return dictionary.get(word.toLowerCase());
    
            } else {
                return "This word is not in the dictionary, "
                        + "please use the add function.";
            }
        }
    

    它会起作用的。 希望这会有所帮助。

    【讨论】:

    • 真的吗?有什么理由可以按照您的建议工作吗?
    • 检查他的逻辑。他想要做的是。
    • 是的,我只是错过了 [contains] 和 [containsKey]。 @jitsonfire,好点。仍然字典必须是静态的。
    • 不,它不必是静态的。您将只有 Server 类的单个实例。而且由于您使用 ConcurrentHashMap 作为您的字典,因此您非常安全。
    【解决方案2】:

    Web 服务本质上是无状态的。每个 Web 请求都会有自己的上下文和实例。因此,服务于 port.addWord() 请求的服务器实例可能与服务于 port.getDefinition() 的服务器实例不同。在这种情况下,将结果放入其中的字典映射与用于检索结果的字典映射不同。

    为了使其工作,数据需要以某种方式保存在服务器端。这可以通过数据库来完成。或者,如果您只是出于测试目的这样做,请将字典的定义更改为静态,以便所有 Server 实例共享同一个映射。

    【讨论】:

    • Web 服务本质上是无状态的?谁说那总是真的?还有有状态的服务。 stackoverflow.com/questions/94660/stateful-web-services
    • 我已将字典更改为静态,但我仍然遇到同样的问题,NetBeans 现在还发出警告说字典变量可以是最终变量
    • @MukulGoel Web 服务通常是无状态的。我相信您可以找到一些有状态的 Web 服务,但我使用过的每个服务都是无状态的,这是有充分理由的,主要是 HTTP 协议本身是无状态的(阅读您链接到的问题的答案)。
    • @user4301818 请参见上面的 jitsonfire 答案。您的逻辑存在缺陷(使用 word.toLowerCase() 从字典中检索)。不过,您仍然需要地图是静态的。
    • @AliCheaito:你完全正确。但是 Web 服务仍然不是无状态的。它们可以是无状态的或有状态的,但是是的,无状态的更为常见。在您很少遇到有状态的情况下更常见。
    【解决方案3】:

    字典定义为静态变量。这样,在服务器端创建的每个 Web 服务实例都将使用相同的字典来放置/获取数据。

    private static ConcurrentHashMap<String,String> dictionary;
    

    【讨论】:

    • 我已将字典更改为静态但我仍然遇到同样的问题,NetBeans 现在也给出警告说字典变量可以是最终的
    • 是的,您可以将其设为 [final],因为它永远不会被重新分配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2017-01-26
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多