@WebService public interface BaseService { String sayHi(@WebParam(name="name")String text); }
@WebService(endpointInterface="webservice.BaseService",serviceName="BaseService") public class BaseServiceImpl implements BaseService { public String sayHi(String text) { return "hi : " + text; } }
public class App { public static void main(String[] args) { BaseServiceImpl service = new BaseServiceImpl(); String address = "http://localhost:8080/baseService"; Endpoint.publish(address, service); } }
http://localhost:8080/baseService?wsdl
@WebService(targetNamespace="http://webservice/") public interface IService1 { String sayHi(@WebParam(name="name")String text); }
註解WebService中targetNamespace爲wsdl中的targetNamespace。該屬性默認爲接口包名,若是接口包名與發佈服務接口包名一致,則可不寫。 方法名爲wsdl中wsdl:operation name屬性。 註解WebParam中name爲wsdl中wsdl:input name屬性。若是服務發佈時未用WebParam,則客戶端接口也可忽略。java
@Test public void testIService() { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(IService1.class); svr.setAddress("http://localhost:8080/baseService"); IService1 hw = (IService1) svr.create(); System.out.println(hw.sayHi("abc")); }
cxf 提供工具可直接根據wsdl生成代碼web
wsdl2java -p com.hyc.webservice -d d:\cxfoutput\src -all http://localhost:8080/baseService?wsdl
生成的客戶端接口app
@WebService(targetNamespace = "http://webservice/", name = "BaseService") @XmlSeeAlso({ObjectFactory.class}) public interface BaseService { @WebResult(name = "return", targetNamespace = "") @RequestWrapper(localName = "sayHi", targetNamespace = "http://webservice/", className = "com.hyc.webservice.SayHi") @WebMethod @ResponseWrapper(localName = "sayHiResponse", targetNamespace = "http://webservice/", className = "com.hyc.webservice.SayHiResponse") public java.lang.String sayHi( @WebParam(name = "name", targetNamespace = "") java.lang.String name ); }