cxf 也可以不依赖spring实现webservice ,废话不多讲 ,看代码
package com.servlet; 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; import com.Hello; import com.HelloImpl; public class WebServelet extends CXFNonspringServlet { //主要是这一个继承CXFNonspringServlet private static final long serialVersionUID = 1L; @Override protected void loadBus(ServletConfig sc) { super.loadBus(sc); Bus b=getBus(); BusFactory.setDefaultBus(b); HelloImpl hi=new HelloImpl();//实现类 Serverfactorybean sfb=new Serverfactorybean(); //server工厂 sfb.setServiceClass(Hello.class);// 接口类 sfb.setAddress("/hi"); //服务请求路径 sfb.setServiceBean(hi); sfb.create(); } }web.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>cxfServlet</servlet-name> <servlet-class>com.servlet.WebServelet</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>启动服务的时候就开启webservice接口
调用接口
public static void main(String[] args) { JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean(); factory.setServiceClass(Hello.class); factory.setAddress("http://localhost:8080/Temp1/ws/hi?wsdl"); Hello client = (Hello)factory.create(); System.out.println(client.say("foo")); }