使用Apache CXF和Spring集成创建Web Service

来源:互联网 发布:python简单程序实例 编辑:程序博客网 时间:2024/06/05 18:53

1.创建HelloWorld 接口类

1package com.googlecode.garbagecan.cxfstudy.helloworld;
2import javax.jws.WebParam;
3import javax.jws.WebResult;
4import javax.jws.WebService;
5@WebService
6public interfaceHelloWorld {
7    public@WebResult(name="String")String sayHi(@WebParam(name="text") String text);
8}

 

2.创建HelloWorld实现类

1package com.googlecode.garbagecan.cxfstudy.helloworld;
2public classHelloWorldImpl implementsHelloWorld {
3    publicString sayHi(String name) {
4        String msg ="Hello " + name + "!";
5        System.out.println("Server: "+ msg);
6        returnmsg;
7    }
8}

 

3.修改web.xml文件

01<!DOCTYPE web-app PUBLIC
02 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
03 "http://java.sun.com/dtd/web-app_2_3.dtd" >
04<web-app>
05    <display-name>cxfstudy</display-name>
06    <servlet>
07        <servlet-name>cxf</servlet-name>
08        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
09        <load-on-startup>1</load-on-startup>
10    </servlet>
11    <servlet-mapping>
12        <servlet-name>cxf</servlet-name>
13        <url-pattern>/ws/*</url-pattern>
14    </servlet-mapping>
15    <listener>
16        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17    </listener>
18     
19    <context-param>
20        <param-name>contextConfigLocation</param-name>
21        <param-value>classpath*:**/spring.xml</param-value>
22    </context-param>
23     
24</web-app>

 

4.创建spring配置文件并放在classpath路径下

01<?xmlversion="1.0"encoding="UTF-8"?>
02<beansxmlns="http://www.springframework.org/schema/beans"
03    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"
04    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
05http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
06    <importresource="classpath:META-INF/cxf/cxf.xml"/>
07    <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
08    <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
09    <jaxws:endpointid="helloworld"implementor="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl"address="/HelloWorld"/>
10     
11    <!-- For client test -->
12    <jaxws:clientid="helloworldClient"address="http://localhost:9000/ws/HelloWorld"serviceClass="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld"/>  
13</beans>

 

5.创建测试类

01package com.googlecode.garbagecan.cxfstudy.helloworld;
02import org.springframework.context.ApplicationContext;
03import org.springframework.context.support.ClassPathXmlApplicationContext;
04public classSpringClient {
05    publicstatic void main(String[] args) {
06        ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
07        HelloWorld helloworld = (HelloWorld)context.getBean("helloworldClient");
08        System.out.println(helloworld.sayHi("kongxx"));
09    }
10}

 

6.测试

6. 1.首先启动tomcat或者使用maven的jetty,并访问http://localhost:9000/ws/HelloWorld?wsdl来验证web service已经启动并且生效;
6. 2.然后运行测试类来验证web service。