由于项目需要,查阅了一下Webservice相关的文档,使用XFire服务引擎,SOAP协议完成了简单的交互。其中,学到了修改接口方法参数名称的方法,写出来分享一下!
WebServices接口的描述文件WSDL中经常出现name="in0"如下:
<
xsd:element
maxOccurs
="
1
"
minOccurs
="
1
"
name="in0"
nillable
="
true
"
type
="
xsd:string
"
/>
<
xsd:element
maxOccurs
="
1
"
minOccurs
="
1
"
name
="in1"
nillable
="
true
"
type
="
xsd:string
"
/>
<
xsd:element
maxOccurs
="
1
"
minOccurs
="
1
"
name
="in2"
nillable
="
true
"
type
="
xsd:string
"
/>
这样对于客户端来说太不方便了需要对着接口文档才知道每个参数的名字。如何让这些不友好的名称改成友好的参数名
name="destnum"
呢?以下有两种方法:
1.XML配置文件:
在接口类同一包下面建一个接口类名称.aegis.xml 的文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping>
<method name="receive">
<parameter index="0" mappedName="destnum" />
<parameter index="1" mappedName="srcnum" />
<parameter index="2" mappedName="msg" />
<parameter index="3" mappedName="msgtype" />
</method>
</mapping>
</mappings>
其中,
name为接口中方法名称,
index为方法中参数的序号,
mappedName为替换后的名称
。
2.JSR181注释配置(也称annotation方式)
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface SMSstaterepReceive {
@WebMethod
@WebResult(name="result") //返回值的name
public String receive(@WebParam(name="destnum")String destnum,
@WebParam(name="srcnum")String srcnum,
@WebParam(name="msg")String msg,
@WebParam(name="msgtype")int msgtype);
//接收接口;@WebParam 参数name
}
services.xml配置文件:
<service>
<name>SMSreceiveService</name>
<namespace>http://scommnet.com/service/SMSreceiveService</namespace>
<serviceClass>com.scommnet.sms.extend.scommnet.webservices.service.SMSstaterepReceive</serviceClass>
<implementationClass>com.scommnet.sms.extend.scommnet.webservices.service.impl.SMSstaterepReceiveImpl</implementationClass>
<serviceFactory>jsr181</serviceFactory> <!--这个必须配置,要不还是显示in0-->
</service>
以上,大家共同学习!
引用文章: http://www.cnblogs.com/talk/archive/2012/03/22/2411147.html