首先编写接口:
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface RosterService {
public List getAllFriends();
public boolean isCustomer(@WebParam(name="customerJid")String customerJid);
}
在写其实现类:
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
@WebService(name="RosterController",endpointInterface = "com.oocl.euc.jabber.service.RosterService",targetNamespace="http://www.jabber.com/webService",serviceName="RosterService")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class RosterServiceImpl implements RosterService {
/**
* get all contacts' jid
*
*/
@WebMethod(operationName="toGetAllFrends",action="getAllFrends",exclude=false)
@WebResult(partName="friendsList")
public List getAllFriends() {
Collection rosters = App.connection.getRoster().getEntries();
List contactJids = new ArrayList();
for (RosterEntry rosterEntry : rosters) {
contactJids.add(rosterEntry.getUser());
}
return contactJids;
}
/**
* judge the contact is Customer or not
* @param customerJid
* @return
*/
@WebMethod(operationName="toIsCustomer",action="isCustomer",exclude=false)
@WebResult(partName="isCustomer")
public boolean isCustomer(@WebParam(name="customerJid")String customerJid){
List contactJids = getAllFriends();
boolean isCustomer = true;
for(String contactJid:contactJids){
if(contactJid.equals(customerJid)){
isCustomer = false;
}
}
return isCustomer;
}
}
发布代码:
public static void main(String[] args) {
connection = ConnectionUtil.getConnection(connection);
rosterService = new RosterServiceImpl();
roomService = new RoomServiceImpl();
// Endpoint endpoint = Endpoint.publish("http://localhost:8080/jabber", rosterService);
Endpoint.publish("http://localhost:8080/jabber", rosterService);
/*
}