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

ecoolper
118 声望2 粉丝

引用和评论

0 条评论