cxf发布webservice示例

1. 新建一个web工程,并导入相关的lib

     依赖jar文件:
      axiom-api-1.2.7.jar
      axis2-kernel-1.4.1.jar
      jboss-backport-concurrent.jar
      commons-httpclient-3.1.jar
      commons-codec-1.3.jar
     axiom-impl-1.2.7.jar
2.新建两个类HelloService和HelloServiceImpl

package com.pwp.webservice.service;

import javax.jws.WebMethod;
import javax.jws.WebService;
/**
 *
 * @author Administrator
 */
@WebService
public interface HelloService {
    @WebMethod
    String sayHi(String name);

}

 

package com.pwp.webservice.service.impl;
import com.pwp.webservice.service.HelloService;
import javax.jws.WebService;
/**
 *
 * @author Administrator
 */
@WebService(endpointInterface = "com.pwp.webservice.service.HelloService")
public class HelloServiceImpl implements HelloService{
    public String sayHi(String name) {
        return name+" hello~!";
    }
}

 3.在web-inf目录新赠一个bean.xml内容如下

 

<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:jaxrs="http://cxf.apache.org/jaxrs"
      xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

    <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-extension-jaxrs-binding.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint
	  id="helloWorld"
	  implementor="com.pwp.webservice.service.impl.HelloServiceImpl"
	  address="/HelloWorld" />

</beans>

 4.配置web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <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>/ws/*</url-pattern>
    </servlet-mapping>

 

 

你可能感兴趣的:(webservice)