java基于CXF框架发布webservice服务

先到cxf官网下载相关jar包   http://cxf.apache.org/download.html

然后新建项目把相应的jar包导入进去,在web.xml中配置cxf的servlet

  
    <!-- 配置cxf框架提供的servlet -->
  <servlet>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <!-- 初始化配置文件位置,默认在web-inf下面 -->
      <init-param>
          <param-name>config-location</param-name>
          <param-value>classpath:cxf.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/service/*</url-pattern>
  </servlet-mapping> 

然后导入cxf的配置文件,其实就是spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://cxf.apache.org/bindings/soap 
                    http://cxf.apache.org/schemas/configuration/soap.xsd
                    http://cxf.apache.org/jaxws 
                    http://cxf.apache.org/schemas/jaxws.xsd">
    <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    <bean id="helloService" class="com.bgs.service.HelloServiceImpl"/>
    
    <!-- 注册服务 -->
    <jaxws:server id="myService" address="/cxfService">
        <jaxws:serviceBean>
            <ref bean="helloService"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

 然后写个接口实现类

package com.bgs.service;

import javax.jws.WebService;

@WebService
public interface HelloService {
    public String sayHello(String name);
}
 

package com.bgs.service;

public class HelloServiceImpl implements HelloService{

    @Override
    public String sayHello(String name) {
        System.out.println("调用了");
        return "hello"+name;
    }
    
}
 

然后在配置文件中注入对象即服务就可以访问了 

访问路径就是ip地址加tomcat的端口号,加项目名,加web.xml中配置的servlet的访问路径,加服务名,再加?wsdl

猜你喜欢

转载自blog.csdn.net/kxj19980524/article/details/84675258