报了这样一个错误:(javax.xml.rpc.ServiceException: Only interfaces may be used for the proxy class argument at org.apache.axis.client.Service.getPort(Service.java:405) at org.apache.axis.client.Service.getPort(Service.java:312) at b2b.TestWebService.main(TestWebService.java:65)Exception in thread "main"
例如:现在有一个提供天气预报的webservice服务“http://webservicexx.net/webservice/Weather.asmx?WSDL”根据此可以得到Weather.java,WeatherLocator.java,WeatherSoap.java,WeatherSoapStub.java到本地,如下:Weather.java: package net.webservicexx.webservice;public interface Weather extends javax.xml.rpc.Service { public java.lang.String getWeatherSoapAddress(); public net.webservicexx.webservice.WeatherSoap getWeatherSoap() throws javax.xml.rpc.ServiceException; public net.webservicexx.webservice.WeatherSoap getWeatherSoap(java.net.URL portAddress) throws javax.xml.rpc.ServiceException; } WeatherLocator.java: package net.webservicexx.webservice;public class WeatherLocator extends org.apache.axis.client.Service implements net.webservicexx.webservice.Weather { // Use to get a proxy class for WeatherSoap private final java.lang.String WeatherSoap_address = "http://webservicexx.net/webservice/Weather.asmx"; public java.lang.String getWeatherSoapAddress() { return WeatherSoap_address; } // The WSDD service name defaults to the port name. private java.lang.String WeatherSoapWSDDServiceName = "WeatherSoap"; public java.lang.String getWeatherSoapWSDDServiceName() { return WeatherSoapWSDDServiceName; } public void setWeatherSoapWSDDServiceName(java.lang.String name) { WeatherSoapWSDDServiceName = name; } public net.webservicexx.webservice.WeatherSoap getWeatherSoap() throws javax.xml.rpc.ServiceException { java.net.URL endpoint; try { endpoint = new java.net.URL(WeatherSoap_address); } catch (java.net.MalformedURLException e) { throw new javax.xml.rpc.ServiceException(e); } return getWeatherSoap(endpoint); } public net.webservicexx.webservice.WeatherSoap getWeatherSoap(java.net.URL portAddress) throws javax.xml.rpc.ServiceException { try { net.webservicexx.webservice.WeatherSoapStub _stub = new net.webservicexx.webservice.WeatherSoapStub(portAddress, this); _stub.setPortName(getWeatherSoapWSDDServiceName()); return _stub; } catch (org.apache.axis.AxisFault e) { return null; } } /** * For the given interface, get the stub implementation. * If this service has no port for the given interface, * then ServiceException is thrown. */ public java.rmi.Remote getPort(Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException { try { if (net.webservicexx.webservice.WeatherSoap.class.isAssignableFrom(serviceEndpointInterface)) { net.webservicexx.webservice.WeatherSoapStub _stub = new net.webservicexx.webservice.WeatherSoapStub(new java.net.URL(WeatherSoap_address), this); _stub.setPortName(getWeatherSoapWSDDServiceName()); return _stub; } } catch (java.lang.Throwable t) { throw new javax.xml.rpc.ServiceException(t); } throw new javax.xml.rpc.ServiceException("There is no stub implementation for the interface: " + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName())); } /** * For the given interface, get the stub implementation. * If this service has no port for the given interface, * then ServiceException is thrown. */ public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException { if (portName == null) { return getPort(serviceEndpointInterface); } String inputPortName = portName.getLocalPart(); if ("WeatherSoap".equals(inputPortName)) { return getWeatherSoap(); } else { java.rmi.Remote _stub = getPort(serviceEndpointInterface); ((org.apache.axis.client.Stub) _stub).setPortName(portName); return _stub; } } public javax.xml.namespace.QName getServiceName() { return new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "Weather"); } private java.util.HashSet ports = null; public java.util.Iterator getPorts() { if (ports == null) { ports = new java.util.HashSet(); ports.add(new javax.xml.namespace.QName("WeatherSoap")); } return ports.iterator(); }}未完待续............................
接上...........................WeatherSoap.java: package net.webservicexx.webservice;public interface WeatherSoap extends java.rmi.Remote { public java.lang.String getWeatherInfo(java.lang.String cityName, java.lang.String loginId) throws java.rmi.RemoteException; }WeatherSoapStub.java: package net.webservicexx.webservice;public class WeatherSoapStub extends org.apache.axis.client.Stub implements net.webservicexx.webservice.WeatherSoap { private java.util.Vector cachedSerClasses = new java.util.Vector(); private java.util.Vector cachedSerQNames = new java.util.Vector(); private java.util.Vector cachedSerFactories = new java.util.Vector(); private java.util.Vector cachedDeserFactories = new java.util.Vector(); static org.apache.axis.description.OperationDesc [] _operations; static { _operations = new org.apache.axis.description.OperationDesc[1]; org.apache.axis.description.OperationDesc oper; oper = new org.apache.axis.description.OperationDesc(); oper.setName("GetWeatherInfo"); oper.addParameter(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "cityName"), new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, org.apache.axis.description.ParameterDesc.IN, false, false); oper.addParameter(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "loginId"), new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, org.apache.axis.description.ParameterDesc.IN, false, false); oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string")); oper.setReturnClass(java.lang.String.class); oper.setReturnQName(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "GetWeatherInfoResult")); oper.setStyle(org.apache.axis.enum.Style.WRAPPED); oper.setUse(org.apache.axis.enum.Use.LITERAL); _operations[0] = oper; } public WeatherSoapStub() throws org.apache.axis.AxisFault { this(null); } public WeatherSoapStub(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault { this(service); super.cachedEndpoint = endpointURL; } public WeatherSoapStub(javax.xml.rpc.Service service) throws org.apache.axis.AxisFault { if (service == null) { super.service = new org.apache.axis.client.Service(); } else { super.service = service; } } private org.apache.axis.client.Call createCall() throws java.rmi.RemoteException { try { org.apache.axis.client.Call _call = (org.apache.axis.client.Call) super.service.createCall(); if (super.maintainSessionSet) { _call.setMaintainSession(super.maintainSession); } if (super.cachedUsername != null) { _call.setUsername(super.cachedUsername); } if (super.cachedPassword != null) { _call.setPassword(super.cachedPassword); } if (super.cachedEndpoint != null) { _call.setTargetEndpointAddress(super.cachedEndpoint); } if (super.cachedTimeout != null) { _call.setTimeout(super.cachedTimeout); } if (super.cachedPortName != null) { _call.setPortName(super.cachedPortName); } java.util.Enumeration keys = super.cachedProperties.keys(); while (keys.hasMoreElements()) { java.lang.String key = (java.lang.String) keys.nextElement(); _call.setProperty(key, super.cachedProperties.get(key)); } return _call; } catch (java.lang.Throwable t) { throw new org.apache.axis.AxisFault("Failure trying to get the Call object", t); } } public java.lang.String getWeatherInfo(java.lang.String cityName, java.lang.String loginId) throws java.rmi.RemoteException { if (super.cachedEndpoint == null) { throw new org.apache.axis.NoEndPointException(); } org.apache.axis.client.Call _call = createCall(); _call.setOperation(_operations[0]); _call.setUseSOAPAction(true); _call.setSOAPActionURI("http://webservicexx.net/webservice/GetWeatherInfo"); _call.setEncodingStyle(null); _call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE); _call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE); _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS); _call.setOperationName(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "GetWeatherInfo")); setRequestHeaders(_call); setAttachments(_call); java.lang.Object _resp = _call.invoke(new java.lang.Object[] {cityName, loginId}); if (_resp instanceof java.rmi.RemoteException) { throw (java.rmi.RemoteException)_resp; } else { extractAttachments(_call); try { return (java.lang.String) _resp; } catch (java.lang.Exception _exception) { return (java.lang.String) org.apache.axis.utils.JavaUtils.convert(_resp, java.lang.String.class); } } }} 然后写测试类如下: package net.webservicexx.webservice;import java.rmi.*;public class Test { public Test() { } public static void main(String[] args) { net.webservicexx.webservice.WeatherSoapStub binding; try { binding = (net.webservicexx.webservice.WeatherSoapStub) new net.webservicexx.webservice.WeatherLocator().getWeatherSoap(); } catch (javax.xml.rpc.ServiceException jre) { if (jre.getLinkedCause() != null) { jre.getLinkedCause().printStackTrace(); } throw new junit.framework.AssertionFailedError( "JAX-RPC ServiceException caught: " + jre); } // Time out after a minute binding.setTimeout(60000); // Test operation java.lang.String value = null; try { value = binding.getWeatherInfo("深圳", "000000002005010100000001"); System.out.println("======天气预报======="+value); } catch (RemoteException ex) { } } }就可以得到天气了!!!
报了这样一个错误:(javax.xml.rpc.ServiceException: Only interfaces may be used for the proxy class argument>>>> 错误描述很清楚啊 CityService city = (CityService) service.getPort(portName, CityService.class); 这里的CityService一定得是接口来的(我前面有给出这个代码) 因为java的动态代理只支持接口,而不是一般类型
楼上joybo() 的方法是根据WSDL2Java工具,通过已有的wsdl文件来自动生成相应的client接口和stub 这也是一种简单易用的办法还是比如这个CityService,通过axis的WSDL2Java,可以生成四个类(包括接口): CityService.java CityServiceService.java CityServiceServiceLocator.java CitySoapBindingStub.java然后可以在TestCity.java中如此调用public class TestCity { public static void main(String[] args) throws Exception { CityServiceServiceLocator loc = new CityServiceServiceLocator(); CityService cs = loc.getCity(); System.out.println("the zip code: " + cs.getZip()); System.out.println("the tel code: " + cs.getTel()); } }简单吧?
不对,我可以把我的这个几个自动生成的类(接口给你看,调用的时候有问题),实在是不好意思啊,java这里几乎一无所知。 SimpleTest.java public interface SimpleTest extends java.rmi.Remote { public test.generated.Message process(test.generated.Message in0) throws java.rmi.RemoteException; }SimpleTestServic.java public interface SimpleTestService extends javax.xml.rpc.Service { public java.lang.String getSimpleTestAddress(); public test.generated.SimpleTest getSimpleTest() throws javax.xml.rpc.ServiceException; public test.generated.SimpleTest getSimpleTest(java.net.URL portAddress) throws javax.xml.rpc.ServiceException; } SimpleTestServiceLocator.java .......SimpleTestSoapBindingStub ............. 然后我建了一个class,按照你那样的写就有问题,请多多指教啊
SimpleTestServiceLocator.javapublic class SimpleTestServiceLocator extends org.apache.axis.client.Service implements test.generated.SimpleTestService { // Use to get a proxy class for SimpleTest private java.lang.String SimpleTest_address = "http://localhost:8080/t/services/SimpleTest"; public java.lang.String getSimpleTestAddress() { return SimpleTest_address; } // The WSDD service name defaults to the port name. private java.lang.String SimpleTestWSDDServiceName = "SimpleTest"; public java.lang.String getSimpleTestWSDDServiceName() { return SimpleTestWSDDServiceName; } public void setSimpleTestWSDDServiceName(java.lang.String name) { SimpleTestWSDDServiceName = name; } public test.generated.SimpleTest getSimpleTest() throws javax.xml.rpc.ServiceException { java.net.URL endpoint; try { endpoint = new java.net.URL(SimpleTest_address); } catch (java.net.MalformedURLException e) { throw new javax.xml.rpc.ServiceException(e); } return getSimpleTest(endpoint); } public test.generated.SimpleTest getSimpleTest(java.net.URL portAddress) throws javax.xml.rpc.ServiceException { try { test.generated.SimpleTestSoapBindingStub _stub = new test.generated.SimpleTestSoapBindingStub(portAddress, this); _stub.setPortName(getSimpleTestWSDDServiceName()); return _stub; } catch (org.apache.axis.AxisFault e) { return null; } } public void setSimpleTestEndpointAddress(java.lang.String address) { SimpleTest_address = address; } /** * For the given interface, get the stub implementation. * If this service has no port for the given interface, * then ServiceException is thrown. */ public java.rmi.Remote getPort(Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException { try { if (test.generated.SimpleTest.class.isAssignableFrom(serviceEndpointInterface)) { test.generated.SimpleTestSoapBindingStub _stub = new test.generated.SimpleTestSoapBindingStub(new java.net.URL(SimpleTest_address), this); _stub.setPortName(getSimpleTestWSDDServiceName()); return _stub; } } catch (java.lang.Throwable t) { throw new javax.xml.rpc.ServiceException(t); } throw new javax.xml.rpc.ServiceException("There is no stub implementation for the interface: " + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName())); } /** * For the given interface, get the stub implementation. * If this service has no port for the given interface, * then ServiceException is thrown. */ public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException { if (portName == null) { return getPort(serviceEndpointInterface); } java.lang.String inputPortName = portName.getLocalPart(); if ("SimpleTest".equals(inputPortName)) { return getSimpleTest(); } else { java.rmi.Remote _stub = getPort(serviceEndpointInterface); ((org.apache.axis.client.Stub) _stub).setPortName(portName); return _stub; } } public javax.xml.namespace.QName getServiceName() { return new javax.xml.namespace.QName("http://test", "SimpleTestService"); } private java.util.HashSet ports = null; public java.util.Iterator getPorts() { if (ports == null) { ports = new java.util.HashSet(); ports.add(new javax.xml.namespace.QName("http://test", "SimpleTest")); } return ports.iterator(); } /** * Set the endpoint address for the specified port name. */ public void setEndpointAddress(java.lang.String portName, java.lang.String address) throws javax.xml.rpc.ServiceException { if ("SimpleTest".equals(portName)) { setSimpleTestEndpointAddress(address); } else { // Unknown Port Name throw new javax.xml.rpc.ServiceException(" Cannot set Endpoint Address for Unknown Port" + portName); } }
// All the type mapping information is registered // when the first call is made. // The type mapping information is actually registered in // the TypeMappingRegistry of the service, which // is the reason why registration is only needed for the first call. synchronized (this) { if (firstCall()) { // must set encoding style before registering serializers _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS); _call.setEncodingStyle(org.apache.axis.Constants.URI_SOAP11_ENC); for (int i = 0; i < cachedSerFactories.size(); ++i) { java.lang.Class cls = (java.lang.Class) cachedSerClasses.get(i); javax.xml.namespace.QName qName = (javax.xml.namespace.QName) cachedSerQNames.get(i); java.lang.Class sf = (java.lang.Class) cachedSerFactories.get(i); java.lang.Class df = (java.lang.Class) cachedDeserFactories.get(i); _call.registerTypeMapping(cls, qName, sf, df, false); } } } return _call; } catch (java.lang.Throwable _t) { throw new org.apache.axis.AxisFault("Failure trying to get the Call object", _t); } } public test.generated.Message process(test.generated.Message in0) throws java.rmi.RemoteException { if (super.cachedEndpoint == null) { throw new org.apache.axis.NoEndPointException(); } org.apache.axis.client.Call _call = createCall(); _call.setOperation(_operations[0]); _call.setUseSOAPAction(true); _call.setSOAPActionURI(""); _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS); _call.setOperationName(new javax.xml.namespace.QName("http://test", "process")); setRequestHeaders(_call); setAttachments(_call); java.lang.Object _resp = _call.invoke(new java.lang.Object[] {in0}); if (_resp instanceof java.rmi.RemoteException) { throw (java.rmi.RemoteException)_resp; } else { extractAttachments(_call); try { return (test.generated.Message) _resp; } catch (java.lang.Exception _exception) { return (test.generated.Message) org.apache.axis.utils.JavaUtils.convert(_resp, test.generated.Message.class); } } }}
我发布的service和相关类如下: 发布的类及其方法: public class SimpleTest { public SimpleTest() { }
return m; } } 相关的类:public class Message { public Property[] properties; public Message() { } } public class Property { public String key; public String value; public Property() { } }
我写的调用的类如下: public class testjavaweb { public static void main(String[] args) throws ServiceException { SimpleTestServiceLocator loc = new SimpleTestServiceLocator(); SimpleTest cs = loc.getSimpleTest(); Message g = new Message(); Message r = new Message(); Property k = new Property(); k.key = "afe"; k.value = "dd"; g.properties = new Property[] {k}; r=cs.process(g); //就是这里有错,"testjavaweb.java": process(test.generated.Message) in test.generated.SimpleTest cannot be applied to (test.Message) at line 33, column 15 System.out.println(r.properties.length); } public testjavaweb() { } }
String namespace = "http://Hello.com";
String serviceName = "HelloWebService";
QName serviceQN = new QName(namespace, serviceName);
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service service = serviceFactory.createService(new URL(wsdlURL), serviceQN);
然后呢,或者这样做本来就是错误的!
String ns = "service.ws.zcjl.com";
String serviceName = "CityServiceService";
QName qName = new QName(ns, serviceName);
ServiceFactory sf = ServiceFactory.newInstance();
Service service = sf.createService(new URL(endpointURL), qName);QName portName = new QName("", "City");
CityService city = (CityService) service.getPort(portName, CityService.class);
System.out.println(city.getZip());
其中CityService.java如下:
package com.zcjl.ws.service;public interface CityService extends java.rmi.Remote {
public java.lang.String getZip() throws java.rmi.RemoteException;
public java.lang.String getTel() throws java.rmi.RemoteException;
}
******就是上面所标出的部署的service Name
http://www-900.ibm.com/developerWorks/cn/webservices/ws-tip-soapjax/index.shtml
写的
package net.webservicexx.webservice;public interface Weather extends javax.xml.rpc.Service {
public java.lang.String getWeatherSoapAddress(); public net.webservicexx.webservice.WeatherSoap getWeatherSoap() throws javax.xml.rpc.ServiceException; public net.webservicexx.webservice.WeatherSoap getWeatherSoap(java.net.URL portAddress) throws javax.xml.rpc.ServiceException;
}
WeatherLocator.java:
package net.webservicexx.webservice;public class WeatherLocator extends org.apache.axis.client.Service implements net.webservicexx.webservice.Weather { // Use to get a proxy class for WeatherSoap
private final java.lang.String WeatherSoap_address = "http://webservicexx.net/webservice/Weather.asmx"; public java.lang.String getWeatherSoapAddress() {
return WeatherSoap_address;
} // The WSDD service name defaults to the port name.
private java.lang.String WeatherSoapWSDDServiceName = "WeatherSoap"; public java.lang.String getWeatherSoapWSDDServiceName() {
return WeatherSoapWSDDServiceName;
} public void setWeatherSoapWSDDServiceName(java.lang.String name) {
WeatherSoapWSDDServiceName = name;
} public net.webservicexx.webservice.WeatherSoap getWeatherSoap() throws javax.xml.rpc.ServiceException {
java.net.URL endpoint;
try {
endpoint = new java.net.URL(WeatherSoap_address);
}
catch (java.net.MalformedURLException e) {
throw new javax.xml.rpc.ServiceException(e);
}
return getWeatherSoap(endpoint);
} public net.webservicexx.webservice.WeatherSoap getWeatherSoap(java.net.URL portAddress) throws javax.xml.rpc.ServiceException {
try {
net.webservicexx.webservice.WeatherSoapStub _stub = new net.webservicexx.webservice.WeatherSoapStub(portAddress, this);
_stub.setPortName(getWeatherSoapWSDDServiceName());
return _stub;
}
catch (org.apache.axis.AxisFault e) {
return null;
}
} /**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
try {
if (net.webservicexx.webservice.WeatherSoap.class.isAssignableFrom(serviceEndpointInterface)) {
net.webservicexx.webservice.WeatherSoapStub _stub = new net.webservicexx.webservice.WeatherSoapStub(new java.net.URL(WeatherSoap_address), this);
_stub.setPortName(getWeatherSoapWSDDServiceName());
return _stub;
}
}
catch (java.lang.Throwable t) {
throw new javax.xml.rpc.ServiceException(t);
}
throw new javax.xml.rpc.ServiceException("There is no stub implementation for the interface: " + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName()));
} /**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
if (portName == null) {
return getPort(serviceEndpointInterface);
}
String inputPortName = portName.getLocalPart();
if ("WeatherSoap".equals(inputPortName)) {
return getWeatherSoap();
}
else {
java.rmi.Remote _stub = getPort(serviceEndpointInterface);
((org.apache.axis.client.Stub) _stub).setPortName(portName);
return _stub;
}
} public javax.xml.namespace.QName getServiceName() {
return new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "Weather");
} private java.util.HashSet ports = null; public java.util.Iterator getPorts() {
if (ports == null) {
ports = new java.util.HashSet();
ports.add(new javax.xml.namespace.QName("WeatherSoap"));
}
return ports.iterator();
}}未完待续............................
package net.webservicexx.webservice;public interface WeatherSoap extends java.rmi.Remote {
public java.lang.String getWeatherInfo(java.lang.String cityName, java.lang.String loginId) throws java.rmi.RemoteException;
}WeatherSoapStub.java:
package net.webservicexx.webservice;public class WeatherSoapStub extends org.apache.axis.client.Stub implements net.webservicexx.webservice.WeatherSoap {
private java.util.Vector cachedSerClasses = new java.util.Vector();
private java.util.Vector cachedSerQNames = new java.util.Vector();
private java.util.Vector cachedSerFactories = new java.util.Vector();
private java.util.Vector cachedDeserFactories = new java.util.Vector(); static org.apache.axis.description.OperationDesc [] _operations; static {
_operations = new org.apache.axis.description.OperationDesc[1];
org.apache.axis.description.OperationDesc oper;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("GetWeatherInfo");
oper.addParameter(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "cityName"), new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, org.apache.axis.description.ParameterDesc.IN, false, false);
oper.addParameter(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "loginId"), new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, org.apache.axis.description.ParameterDesc.IN, false, false);
oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "GetWeatherInfoResult"));
oper.setStyle(org.apache.axis.enum.Style.WRAPPED);
oper.setUse(org.apache.axis.enum.Use.LITERAL);
_operations[0] = oper; } public WeatherSoapStub() throws org.apache.axis.AxisFault {
this(null);
} public WeatherSoapStub(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
this(service);
super.cachedEndpoint = endpointURL;
} public WeatherSoapStub(javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
if (service == null) {
super.service = new org.apache.axis.client.Service();
} else {
super.service = service;
}
} private org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {
try {
org.apache.axis.client.Call _call =
(org.apache.axis.client.Call) super.service.createCall();
if (super.maintainSessionSet) {
_call.setMaintainSession(super.maintainSession);
}
if (super.cachedUsername != null) {
_call.setUsername(super.cachedUsername);
}
if (super.cachedPassword != null) {
_call.setPassword(super.cachedPassword);
}
if (super.cachedEndpoint != null) {
_call.setTargetEndpointAddress(super.cachedEndpoint);
}
if (super.cachedTimeout != null) {
_call.setTimeout(super.cachedTimeout);
}
if (super.cachedPortName != null) {
_call.setPortName(super.cachedPortName);
}
java.util.Enumeration keys = super.cachedProperties.keys();
while (keys.hasMoreElements()) {
java.lang.String key = (java.lang.String) keys.nextElement();
_call.setProperty(key, super.cachedProperties.get(key));
}
return _call;
}
catch (java.lang.Throwable t) {
throw new org.apache.axis.AxisFault("Failure trying to get the Call object", t);
}
} public java.lang.String getWeatherInfo(java.lang.String cityName, java.lang.String loginId) throws java.rmi.RemoteException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[0]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("http://webservicexx.net/webservice/GetWeatherInfo");
_call.setEncodingStyle(null);
_call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
_call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName("http://webservicexx.net/webservice/", "GetWeatherInfo")); setRequestHeaders(_call);
setAttachments(_call);
java.lang.Object _resp = _call.invoke(new java.lang.Object[] {cityName, loginId}); if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)_resp;
}
else {
extractAttachments(_call);
try {
return (java.lang.String) _resp;
} catch (java.lang.Exception _exception) {
return (java.lang.String) org.apache.axis.utils.JavaUtils.convert(_resp, java.lang.String.class);
}
}
}}
然后写测试类如下:
package net.webservicexx.webservice;import java.rmi.*;public class Test {
public Test() {
} public static void main(String[] args) {
net.webservicexx.webservice.WeatherSoapStub binding;
try {
binding = (net.webservicexx.webservice.WeatherSoapStub)
new net.webservicexx.webservice.WeatherLocator().getWeatherSoap();
}
catch (javax.xml.rpc.ServiceException jre) {
if (jre.getLinkedCause() != null) {
jre.getLinkedCause().printStackTrace();
}
throw new junit.framework.AssertionFailedError(
"JAX-RPC ServiceException caught: " + jre);
} // Time out after a minute
binding.setTimeout(60000); // Test operation
java.lang.String value = null;
try {
value = binding.getWeatherInfo("深圳",
"000000002005010100000001");
System.out.println("======天气预报======="+value);
}
catch (RemoteException ex) {
} }
}就可以得到天气了!!!
报了这样一个错误:(javax.xml.rpc.ServiceException: Only interfaces may be used for the proxy class argument>>>>
错误描述很清楚啊
CityService city = (CityService) service.getPort(portName, CityService.class);
这里的CityService一定得是接口来的(我前面有给出这个代码)
因为java的动态代理只支持接口,而不是一般类型
这也是一种简单易用的办法还是比如这个CityService,通过axis的WSDL2Java,可以生成四个类(包括接口):
CityService.java
CityServiceService.java
CityServiceServiceLocator.java
CitySoapBindingStub.java然后可以在TestCity.java中如此调用public class TestCity {
public static void main(String[] args) throws Exception {
CityServiceServiceLocator loc = new CityServiceServiceLocator();
CityService cs = loc.getCity();
System.out.println("the zip code: " + cs.getZip());
System.out.println("the tel code: " + cs.getTel());
}
}简单吧?
SimpleTest.java
public interface SimpleTest extends java.rmi.Remote {
public test.generated.Message process(test.generated.Message in0) throws java.rmi.RemoteException;
}SimpleTestServic.java
public interface SimpleTestService extends javax.xml.rpc.Service {
public java.lang.String getSimpleTestAddress(); public test.generated.SimpleTest getSimpleTest() throws javax.xml.rpc.ServiceException; public test.generated.SimpleTest getSimpleTest(java.net.URL portAddress) throws javax.xml.rpc.ServiceException;
}
SimpleTestServiceLocator.java
.......SimpleTestSoapBindingStub
.............
然后我建了一个class,按照你那样的写就有问题,请多多指教啊
>>>>
关键是这个类的内容,还有错误信息
你不提供给我,我也不知道哪里有问题啊
private java.lang.String SimpleTest_address = "http://localhost:8080/t/services/SimpleTest"; public java.lang.String getSimpleTestAddress() {
return SimpleTest_address;
} // The WSDD service name defaults to the port name.
private java.lang.String SimpleTestWSDDServiceName = "SimpleTest"; public java.lang.String getSimpleTestWSDDServiceName() {
return SimpleTestWSDDServiceName;
} public void setSimpleTestWSDDServiceName(java.lang.String name) {
SimpleTestWSDDServiceName = name;
} public test.generated.SimpleTest getSimpleTest() throws javax.xml.rpc.ServiceException {
java.net.URL endpoint;
try {
endpoint = new java.net.URL(SimpleTest_address);
}
catch (java.net.MalformedURLException e) {
throw new javax.xml.rpc.ServiceException(e);
}
return getSimpleTest(endpoint);
} public test.generated.SimpleTest getSimpleTest(java.net.URL portAddress) throws javax.xml.rpc.ServiceException {
try {
test.generated.SimpleTestSoapBindingStub _stub = new test.generated.SimpleTestSoapBindingStub(portAddress, this);
_stub.setPortName(getSimpleTestWSDDServiceName());
return _stub;
}
catch (org.apache.axis.AxisFault e) {
return null;
}
} public void setSimpleTestEndpointAddress(java.lang.String address) {
SimpleTest_address = address;
} /**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
try {
if (test.generated.SimpleTest.class.isAssignableFrom(serviceEndpointInterface)) {
test.generated.SimpleTestSoapBindingStub _stub = new test.generated.SimpleTestSoapBindingStub(new java.net.URL(SimpleTest_address), this);
_stub.setPortName(getSimpleTestWSDDServiceName());
return _stub;
}
}
catch (java.lang.Throwable t) {
throw new javax.xml.rpc.ServiceException(t);
}
throw new javax.xml.rpc.ServiceException("There is no stub implementation for the interface: " + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName()));
} /**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
if (portName == null) {
return getPort(serviceEndpointInterface);
}
java.lang.String inputPortName = portName.getLocalPart();
if ("SimpleTest".equals(inputPortName)) {
return getSimpleTest();
}
else {
java.rmi.Remote _stub = getPort(serviceEndpointInterface);
((org.apache.axis.client.Stub) _stub).setPortName(portName);
return _stub;
}
} public javax.xml.namespace.QName getServiceName() {
return new javax.xml.namespace.QName("http://test", "SimpleTestService");
} private java.util.HashSet ports = null; public java.util.Iterator getPorts() {
if (ports == null) {
ports = new java.util.HashSet();
ports.add(new javax.xml.namespace.QName("http://test", "SimpleTest"));
}
return ports.iterator();
} /**
* Set the endpoint address for the specified port name.
*/
public void setEndpointAddress(java.lang.String portName, java.lang.String address) throws javax.xml.rpc.ServiceException {
if ("SimpleTest".equals(portName)) {
setSimpleTestEndpointAddress(address);
}
else { // Unknown Port Name
throw new javax.xml.rpc.ServiceException(" Cannot set Endpoint Address for Unknown Port" + portName);
}
}
public class SimpleTestSoapBindingStub extends org.apache.axis.client.Stub implements test.generated.SimpleTest {
private java.util.Vector cachedSerClasses = new java.util.Vector();
private java.util.Vector cachedSerQNames = new java.util.Vector();
private java.util.Vector cachedSerFactories = new java.util.Vector();
private java.util.Vector cachedDeserFactories = new java.util.Vector(); static org.apache.axis.description.OperationDesc [] _operations; static {
_operations = new org.apache.axis.description.OperationDesc[1];
_initOperationDesc1();
} private static void _initOperationDesc1(){
org.apache.axis.description.OperationDesc oper;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("process");
oper.addParameter(new javax.xml.namespace.QName("", "in0"), new javax.xml.namespace.QName("http://test", "Message"), test.generated.Message.class, org.apache.axis.description.ParameterDesc.IN, false, false);
oper.setReturnType(new javax.xml.namespace.QName("http://test", "Message"));
oper.setReturnClass(test.generated.Message.class);
oper.setReturnQName(new javax.xml.namespace.QName("", "processReturn"));
oper.setStyle(org.apache.axis.enum.Style.RPC);
oper.setUse(org.apache.axis.enum.Use.ENCODED);
_operations[0] = oper; } public SimpleTestSoapBindingStub() throws org.apache.axis.AxisFault {
this(null);
} public SimpleTestSoapBindingStub(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
this(service);
super.cachedEndpoint = endpointURL;
} public SimpleTestSoapBindingStub(javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
if (service == null) {
super.service = new org.apache.axis.client.Service();
} else {
super.service = service;
}
java.lang.Class cls;
javax.xml.namespace.QName qName;
java.lang.Class beansf = org.apache.axis.encoding.ser.BeanSerializerFactory.class;
java.lang.Class beandf = org.apache.axis.encoding.ser.BeanDeserializerFactory.class;
java.lang.Class enumsf = org.apache.axis.encoding.ser.EnumSerializerFactory.class;
java.lang.Class enumdf = org.apache.axis.encoding.ser.EnumDeserializerFactory.class;
java.lang.Class arraysf = org.apache.axis.encoding.ser.ArraySerializerFactory.class;
java.lang.Class arraydf = org.apache.axis.encoding.ser.ArrayDeserializerFactory.class;
java.lang.Class simplesf = org.apache.axis.encoding.ser.SimpleSerializerFactory.class;
java.lang.Class simpledf = org.apache.axis.encoding.ser.SimpleDeserializerFactory.class;
java.lang.Class simplelistsf = org.apache.axis.encoding.ser.SimpleListSerializerFactory.class;
java.lang.Class simplelistdf = org.apache.axis.encoding.ser.SimpleListDeserializerFactory.class;
qName = new javax.xml.namespace.QName("http://test", "Message");
cachedSerQNames.add(qName);
cls = test.generated.Message.class;
cachedSerClasses.add(cls);
cachedSerFactories.add(beansf);
cachedDeserFactories.add(beandf); qName = new javax.xml.namespace.QName("http://test", "Property");
cachedSerQNames.add(qName);
cls = test.generated.Property.class;
cachedSerClasses.add(cls);
cachedSerFactories.add(beansf);
cachedDeserFactories.add(beandf); qName = new javax.xml.namespace.QName("http://test", "ArrayOfProperty");
cachedSerQNames.add(qName);
cls = test.generated.Property[].class;
cachedSerClasses.add(cls);
cachedSerFactories.add(arraysf);
cachedDeserFactories.add(arraydf); } private org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {
try {
org.apache.axis.client.Call _call =
(org.apache.axis.client.Call) super.service.createCall();
if (super.maintainSessionSet) {
_call.setMaintainSession(super.maintainSession);
}
if (super.cachedUsername != null) {
_call.setUsername(super.cachedUsername);
}
if (super.cachedPassword != null) {
_call.setPassword(super.cachedPassword);
}
if (super.cachedEndpoint != null) {
_call.setTargetEndpointAddress(super.cachedEndpoint);
}
if (super.cachedTimeout != null) {
_call.setTimeout(super.cachedTimeout);
}
if (super.cachedPortName != null) {
_call.setPortName(super.cachedPortName);
}
java.util.Enumeration keys = super.cachedProperties.keys();
while (keys.hasMoreElements()) {
java.lang.String key = (java.lang.String) keys.nextElement();
_call.setProperty(key, super.cachedProperties.get(key));
}
待续
// when the first call is made.
// The type mapping information is actually registered in
// the TypeMappingRegistry of the service, which
// is the reason why registration is only needed for the first call.
synchronized (this) {
if (firstCall()) {
// must set encoding style before registering serializers
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setEncodingStyle(org.apache.axis.Constants.URI_SOAP11_ENC);
for (int i = 0; i < cachedSerFactories.size(); ++i) {
java.lang.Class cls = (java.lang.Class) cachedSerClasses.get(i);
javax.xml.namespace.QName qName =
(javax.xml.namespace.QName) cachedSerQNames.get(i);
java.lang.Class sf = (java.lang.Class)
cachedSerFactories.get(i);
java.lang.Class df = (java.lang.Class)
cachedDeserFactories.get(i);
_call.registerTypeMapping(cls, qName, sf, df, false);
}
}
}
return _call;
}
catch (java.lang.Throwable _t) {
throw new org.apache.axis.AxisFault("Failure trying to get the Call object", _t);
}
} public test.generated.Message process(test.generated.Message in0) throws java.rmi.RemoteException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[0]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("");
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName("http://test", "process")); setRequestHeaders(_call);
setAttachments(_call);
java.lang.Object _resp = _call.invoke(new java.lang.Object[] {in0}); if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)_resp;
}
else {
extractAttachments(_call);
try {
return (test.generated.Message) _resp;
} catch (java.lang.Exception _exception) {
return (test.generated.Message) org.apache.axis.utils.JavaUtils.convert(_resp, test.generated.Message.class);
}
}
}}
发布的类及其方法:
public class SimpleTest {
public SimpleTest() {
}
public Message process(Message in0){
Message m=new Message();
Property a=new Property();
a.key="afe";
a.value="dd";
m.properties[0]=a;
return m;
}
}
相关的类:public class Message {
public Property[] properties;
public Message() {
}
}
public class Property {
public String key;
public String value;
public Property() {
}
}
public class testjavaweb {
public static void main(String[] args) throws ServiceException { SimpleTestServiceLocator loc = new SimpleTestServiceLocator();
SimpleTest cs = loc.getSimpleTest(); Message g = new Message();
Message r = new Message(); Property k = new Property();
k.key = "afe";
k.value = "dd";
g.properties = new Property[] {k};
r=cs.process(g); //就是这里有错,"testjavaweb.java": process(test.generated.Message) in test.generated.SimpleTest cannot be applied to (test.Message) at line 33, column 15 System.out.println(r.properties.length);
} public testjavaweb() {
}
}
这里的参数g的类型应该是生成的test.generated.Message,而不是在服务端定义的test.Message