Apache CXF实战之二 集成Sping与Web容器[转载] - 全栈笔记

Apache CXF实战之二 集成Sping与Web容器[转载]

任亦伟 JAVA 2015-03-09 112 次浏览 没有评论

书接上文,下面看看CXF怎样和spring集成。

1.创建HelloWorld 接口类

01
package com.googlecode.garbagecan.cxfstudy.helloworld;
02
 
03
import javax.jws.WebMethod;
04
import javax.jws.WebParam;
05
import javax.jws.WebResult;
06
import javax.jws.WebService;
07
 
08
@WebService
09
public interface HelloWorld {
10
    @WebMethod
11
    @WebResult String sayHi(@WebParam String text);
12
}
2.创建HelloWorld实现类
1
package com.googlecode.garbagecan.cxfstudy.helloworld;
2
 
3
public class HelloWorldImpl implements HelloWorld {
4
 
5
    public String sayHi(String name) {
6
        String msg = "Hello " + name + "!";
7
        return msg;
8
    }
9
}
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
 
05
<web-app>
06
 
07
    <display-name>cxfstudy</display-name>
08
 
09
    <servlet>
10
        <servlet-name>cxf</servlet-name>
11
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
12
        <load-on-startup>1</load-on-startup>
13
    </servlet>
14
 
15
    <servlet-mapping>
16
        <servlet-name>cxf</servlet-name>
17
        <url-pattern>/ws/*</url-pattern>
18
    </servlet-mapping>
19
 
20
    <listener>
21
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22
    </listener>
23
 
24
    <context-param>
25
        <param-name>contextConfigLocation</param-name>
26
        <param-value>classpath*:**/spring.xml</param-value>
27
    </context-param>
28
 
29
</web-app>
4.创建spring配置文件并放在classpath路径下

01
<?xml version="1.0" encoding="UTF-8"?>
02
<beans xmlns="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    
05
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
06
    <import resource="classpath:META-INF/cxf/cxf.xml" />
07
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
08
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
09
    <jaxws:endpoint id="helloworld" implementor="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl" address="/HelloWorld" />
10
 
11
    <!-- For client test -->
12
    <jaxws:client id="helloworldClient" address="http://localhost:9000/ws/HelloWorld" serviceClass="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld" />   
13
</beans>
14
5.创建测试类

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

6.1 首先启动tomcat或者使用maven的jetty,并访问http://localhost:9000/ws/HelloWorld?wsdl来验证web service已经启动并且生效;

6.2 然后运行测试类来验证web service。

原文链接:http://blog.csdn.net/kongxx/article/details/7525481

发表评论

回顶部