РЕДАКТИРАНЕ: Проблемът беше двоен, първият речник трябваше да е статичен и също така използвах .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.";
}
}