背景:
使用JDK来开发java web service (Create a SOAP-based RPC style web service endpoint by using JAX-WS)。
具体请看 -> WebService - 使用JDK开发WebService
代码:
import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.MalformedURLException; import java.net.URL; public class WebServiceClient { public static void main(String[] args) throws MalformedURLException { URL url = new URL("http://127.0.0.1:8080/WS_Server/Webservice?wsdl"); QName qname = new QName("http://webservice/", "WebServiceImplService"); Service service = Service.create(url, qname); WebServiceI serviceI = service.getPort(WebServiceI.class); System.out.println(serviceI.sayHello("cuixunxu ")); }
错误:
Exception in thread "main" javax.xml.ws.WebServiceException: Undefined port type: {http://webservice/}WebServiceI at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:456) at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:463) at javax.xml.ws.Service.getPort(Service.java:188) at webservice.client.WebServiceClient.main(WebServiceClient.java:19)
解决方法:
@WebService的接口类WebServiceI代码:
package webservice; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface WebServiceI { @WebMethod String sayHello(String name); @WebMethod String save(String name,String pwd); }
WebServiceI接口的实现类WebServiceImpl类的代码:...注意,需要在@WebService注解后面加上接口的全路径
package webservice; import webservice.WebServiceI; import javax.jws.WebService; @WebService(endpointInterface="webservice.WebServiceI") public class WebServiceImpl implements WebServiceI { @Override public String sayHello(String name) { return name + "is calling!!!"; } @Override public String save(String name, String pwd) { return "Receive name: " + name + " and pwd: " + pwd; } }