@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 ); }