【随记】cxf的webservice接口实现

来源:互联网 发布:aoi检测怎么编程 编辑:程序博客网 时间:2024/06/05 15:44

步骤

1)定义接口。如TestInterface。
2)定义实现类

@WebService(endpointInterface="cn.david.test.TestInterface",targetNamespace="http://test.ws.david.cn/")public class TestDaoImp implements TestInterface {}

3)web.xml配置

    <servlet>        <servlet-name>CXFServlet</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>CXFServlet</servlet-name>        <url-pattern>/cxf/*</url-pattern>    </servlet-mapping>

4)启动服务(可与其他框架整合启动)如果自己启动的话,可以采用以下方式。

 public static void main(String[] args) {          System.out.println("Server is starting...");          TestInterface readerService = new TestDaoImp();          Endpoint.publish("http://test.ws.david.cn/readService",readerService);          System.out.println("Server is started...");      } 

5)客户端访问webservice接口

String address = "http://test.ws.david.cn/readService"; //此处最好用系统参数JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();bean.setAddress(address);   bean.setServiceClass(TestInterface.class);TestInterface ws = (TestInterface) bean.create();System.out.println(ws.getCitiesByCountry("China"));System.out.println(ws.getWeather("Shanghai", "China"));
0 0