阅读更多
客户端
1、所需文件
client.jks,放在工程的src下 (与service.jks是一对,具体生成方法可上网查询)
2、客户端生成方式
从apache官方网站上下载 axis2-1.4.1-bin.zip(如果所用的jar包是1.6的也行),然后解压缩,打开dos命令输入窗口(在运行中输入 cmd ,然后回车即可),从窗口进入到解压缩的文件中,具体到bin目录下,例如:F:\axis2-1.4.1-bin\axis2-1.4.1\bin ,然后输入命令:
wsdl2java -uri http://localhost:8080/test/services/ReceiveMsgService?wsdl -p com.test.client -s -o stub
http://localhost:8080/axis2/services/SimpleService?wsdl 为需要调用的接口地址
com.test.client 为生成的代码所在的包结构
stub 为代码所在文件夹
通过该命令生成的代码保存在 bin 文件夹下,新建一个工程,将代码放入即可;生成的客户端的类为ReceiveMsgServerStub,里面包含了输入输出类,需要调用的方法等。
如果该命令无法生成代码,提示jdk不可用,则表示本机的没有配置java环境变量,需要重新配置环境变量。
3、调用客户端的代码
import java.rmi.RemoteException;
import java.util.Properties;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.description.PolicyInclude;
import org.apache.neethi.Policy;
import org.apache.rampart.policy.model.CryptoConfig;
import org.apache.rampart.policy.model.RampartConfig;
public class ClientTest {
public static void testMsg() throws AxisFault {
ConfigurationContext ctx = ConfigurationContextFactory .createConfigurationContextFromFileSystem("rampart", null);
ReceiveMsgServerStub stub = new ReceiveMsgServerStub(ctx, "http://localhost:8080/test/services/receiveMsgServer");
ServiceClient sc = stub._getServiceClient();
sc.engageModule("rampart");
RampartConfig rampartConfig = new RampartConfig();
rampartConfig.setUser("client");
rampartConfig.setPwCbClass("client.PWCBHandler");
CryptoConfig sigCrypto = new CryptoConfig();
sigCrypto.setProvider("org.apache.ws.security.components.crypto.Merlin");
Properties props = new Properties();
props.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
props.setProperty("org.apache.ws.security.crypto.merlin.file", "client.jks");
props.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "clientPW");
sigCrypto.setProp(props);
rampartConfig.setSigCryptoConfig(sigCrypto);
Policy policy = new Policy();
policy.addAssertion(rampartConfig);
sc.getAxisService().getPolicyInclude().addPolicyElement(PolicyInclude.AXIS_SERVICE_POLICY, policy);
ReceiveMsgServerStub.Message message = new ReceiveMsgServerStub.Message();
message.setMsgId("001");
message.setMsgName("ZHANGRR");
ReceiveMsgServerStub.ReceiveMessage messageRequest = new ReceiveMsgServerStub.ReceiveMessage();
messageRequest.setParam0(message);
ReceiveMsgServerStub.ReceiveMessageResponse response;
try {
response = stub.receiveMessage(messageRequest);
System.out.println(response.get_return());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}