最近开发Xfire发布调用webservice,发现无法传递参数。
问题出在,发布的webservice接口类需要使用如下这种定义方式
?
package com.demo.service;
import javax.jws.WebParam;
public interface MyService {
? String example(
??? @WebParam(targetNamespace = "http://com.demo.service")String agr0,//第一个参数
???????????? @WebParam(targetNamespace = "http://com.demo.service")String agr1,//第二个参数
???????????? @WebParam(targetNamespace = "http://com.demo.service")String agr2,//第三个参数
???????????? @WebParam(targetNamespace = "http://com.demo.service")String agr3//第四个参数,你可以写N个参数
????????????
? );
}
?
当然,用Xfire调用这个webservice的接口时需要用这种方式
?
?
package com.demo.client;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(name = "IMyService",targetNamespace = "http://com.demo.service")
@SOAPBinding(use = SOAPBinding.Use.LITERAL,parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public interface IMyService {
??? @WebMethod(operationName = "example",action = "")
??? @WebResult(name = "return",targetNamespace = "http://com.demo.service")
??? public String example(
??????????? @WebParam(name = "arg0",targetNamespace = "http://com.demo.service")
??????????? String arg0,//第一个参数
??????????? @WebParam(name = "arg1",targetNamespace = "http://com.demo.service")
??????????? String arg1,//第二个参数
??????????? @WebParam(name = "arg2",targetNamespace = "http://com.demo.service")
??????????? String arg2,//第三个参数
??????????? @WebParam(name = "arg3",targetNamespace = "http://com.demo.service")
??????????? String arg3//第四个参数,后面可以加N个参数自己定
??????????? );
?
?
}
?
然后再使用xfire调用方式,就可以传递参数了
?
?
package com.demo.client;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
?public class Test {
? public static void main(String[] args) throws MalformedURLException {
???????? String serviceUrl = "http://localhost:8080/Demo/services/MyService";
????????
???????? Service serviceModel = new AnnotationServiceFactory().create(IMyService.class);
????????
???????? XFire xFire = XFireFactory.newInstance().getXFire();
???????? XFireProxyFactory serviceFactory = new XFireProxyFactory(xFire);
????????
???????? try {
???????? ?IMyService service = (IMyService) serviceFactory.create(serviceModel,
???????????????????? serviceUrl);
???????????? String hello = service.example("001","002","003","004");
???????????? System.out.println(hello);
???????? } catch (Exception e) {
???????????? e.printStackTrace();
???????? }
???????
???? }
?}
?
?
demo源码下载:http://download.csdn.net/detail/zongkai2012/5992713