我的客户端和服务端都写的很简单,只是调用服务的时候,
服务端得不到参数,后来发现只改一个地方就可以了:
call.addParameter("arg0", XMLType.XSD_DATE,ParameterMode.IN); 这样是OK的,
call.addParameter("name", XMLType.XSD_DATE,ParameterMode.IN); 这样服务端接收到的参数就是 null 。
就是红色位置,参数不能写具体的参数名,
要写为: arg0 ,我在网上看到说可以改设置,但是 默认要写为这样才能传参成功。
服务端:
package com.boco.esb.receive;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
//import org.apache.axis2.context.MessageContext;
@WebService(serviceName="HermesService",targetNamespace = "http://com.boco.esb.receive")
public class HermesService {
//该方法就是要暴露给其他应用程序调用的方法
@WebMethod(operationName="example")
public String example(String param){
System.out.println("------------------- 调用服务端方法 !-----------------");
StringBuffer paramStr = new StringBuffer(param);
System.out.println("-----param参数为:"+paramStr);
return param;
}
/**
* 认证
*/
public static void checkHesder() throws Exception
{
}
//这里我们使用main方法来发布我们的service
public static void main(String[] args){
/**
* 参数1:服务的发布地址
* 参数2:服务的实现者
* Endpoint 会重新启动一个线程
*/
Endpoint.publish("http://localhost:7081/Hermes/services/HermesService",new HermesService());
System.out.println("------- com.boco.esb.receive的HDMP的服务启动成功! ----- ~_~ ----- ");
}
}
客户端:
package hdmpClient;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.AxisEngine;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.soap.SOAPConstants;
public class Client extends org.apache.axis.client.Stub{
public static void main(String[] args) {
try {
new Client().get();
} catch (Exception e) {
e.printStackTrace();
}
}
private void get() throws Exception {
Service service = new Service() ;
Call call = (Call) service.createCall();
SOAPHeaderElement serviceCode = new SOAPHeaderElement(new QName("cn.com.boco.HermesService", "ServiceCode"), "SRCH.SERVICE.TEST_GIS.GIS_APP");
SOAPHeaderElement userName = new SOAPHeaderElement(new QName("cn.com.boco.HermesService", "UserName"), "IRMS");
SOAPHeaderElement authCode = new SOAPHeaderElement(new QName("cn.com.boco.HermesService", "AuthCode"), "ca45672f-2c9a-4961-9597-a6f562fdf787");
SOAPHeaderElement eventId = new SOAPHeaderElement(new QName("cn.com.boco.HermesService", "EventId"), "41591591");
call.addHeader(serviceCode);
call.addHeader(userName);
call.addHeader(authCode);
call.addHeader(eventId);
call.setTargetEndpointAddress("http://localhost:7081/Hermes/services/HermesService");
call.setSOAPActionURI("http://com.boco.esb.receive/example");
call.setOperationName(new QName("http://com.boco.esb.receive", "example")); // example 为服务端接口方法名
call.setEncodingStyle(null);
call.setUseSOAPAction(true);
call.setProperty(Call.SEND_TYPE_ATTR, Boolean.FALSE);
call.setProperty(AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
setRequestHeaders(call);
setAttachments(call);
// 设置接口参数与类型( 以下2种都能正常传参 )
// call.addParameter(new QName("arg0"),XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("arg0", XMLType.XSD_DATE,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);// 设置返回数据类型
String[] param = new String[1]; // 参数
param[0] = "参数1,参数2,参数3"; // 实际可用json
String result = (String) call.invoke(param);
System.out.println("result为--------------------"+result);
}
}