CXF和Axis2是两个比较流行的webService框架,接下来我会写几篇博客简单介绍如何使用这两种框架。首先,先简单介绍一下CXF的使用。
CXF发布webService有多种方法,这里我介绍三种:
1、不使用Spring,CXF自动发布webService
2、不使用Spring,CXF手动发布webService
3、使用Spring + CXF发布webService
这篇博客以实例介绍第一种方法——不使用Spring,CXF自动发布webService。
服务端:
目录结构如下:
IHelloWorldServer代码:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.test.server; public interface IHelloWorldServer { public String sayHello(String username); }</span>
HelloWorldServerImp代码:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.test.server; public class HelloWorldServerImp implements IHelloWorldServer { @Override public String sayHello(String username) { return username+" : HelloWorld"; } }</span>
这两个是最最简单的java类。
WebServlet代码:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.test.server; import javax.servlet.ServletConfig; import org.apache.cxf.Bus; import org.apache.cxf.BusFactory; import org.apache.cxf.frontend.ServerFactoryBean; import org.apache.cxf.transport.servlet.CXFNonSpringServlet; public class WebServlet extends CXFNonSpringServlet { // private static final String SERVICE_SUFFIX = ""; private static final long serialVersionUID = 1L; @Override protected void loadBus(ServletConfig servletConfig) { super.loadBus(servletConfig); Bus bus = getBus(); BusFactory.setDefaultBus(bus); HelloWorldServerImp helloWorld = new HelloWorldServerImp();//实现类 ServerFactoryBean serverFactoryBean = new ServerFactoryBean(); //server工厂 serverFactoryBean.setServiceClass(IHelloWorldServer.class);// 接口类 serverFactoryBean.setAddress("/helloWorld"); //服务请求路径 serverFactoryBean.setServiceBean(helloWorld); serverFactoryBean.create(); } }</span>
这个Servlet用于发布webService,当程序部署后,容器会自动调用这里面的方法。
Web.xml文件
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>cxf_demo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <display-name>cxf_demo</display-name> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>com.test.server.WebServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> </web-app></span>
将上述项目发布到Tomcat中,他会自动发布webService,输入网址:http://localhost:8080/cxf_demo_noSpring_1/ws,即可看到发布好的webService,输入网址:http://localhost:8080/cxf_demo_noSpring_1/ws/helloWorld?wsdl,查看wsdl文件,能正常显示,说明webService发布成功。
客户端:
HelloWorldClient代码:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.test.client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class HelloWorldClient { public static void main(String[] args) { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/cxf_demo_noSpring_1/ws/helloWorld?wsdl"); Object[] objects; try { objects = client.invoke("sayHello", "haitao"); //输出调用结果 System.out.println(objects[0].toString()); } catch (Exception e) { e.printStackTrace(); } } } </span>
run之后,结果如下:
至此,第一种方式介绍完毕,接下来的博客我会继续介绍其他方法。
时间: 2024-10-15 22:34:27