码迷,mamicode.com
首页 > 编程语言 > 详细

搭建web项目结合spring+cxf的webservice服务

时间:2014-06-25 15:47:24      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

服务端:

服务端和客户端都需要引入包

bubuko.com,布布扣
 1 antlr-2.7.7.jar
 2 aopalliance-1.0.jar
 3 asm-3.3.jar
 4 commons-collections-3.2.1.jar
 5 commons-lang-2.6.jar
 6 commons-logging-1.1.1.jar
 7 cxf-2.4.2.jar
 8 cxf-manifest.jar
 9 cxf-xjc-boolean-2.4.0.jar
10 cxf-xjc-bug671-2.4.0.jar
11 cxf-xjc-dv-2.4.0.jar
12 cxf-xjc-ts-2.4.0.jar
13 FastInfoset-1.2.9.jar
14 geronimo-activation_1.1_spec-1.1.jar
15 geronimo-annotation_1.0_spec-1.1.1.jar
16 geronimo-javamail_1.4_spec-1.7.1.jar
17 geronimo-jaxws_2.2_spec-1.0.jar
18 geronimo-jms_1.1_spec-1.1.1.jar
19 geronimo-servlet_3.0_spec-1.0.jar
20 geronimo-stax-api_1.0_spec-1.0.1.jar
21 geronimo-ws-metadata_2.0_spec-1.1.3.jar
22 isorelax-20030108.jar
23 jaxb-api-2.2.1.jar
24 jaxb-impl-2.2.1.1.jar
25 jaxb-xjc-2.2.1.1.jar
26 jettison-1.3.jar
27 jetty-continuation-7.4.5.v20110725.jar
28 jetty-http-7.4.5.v20110725.jar
29 jetty-io-7.4.5.v20110725.jar
30 jetty-security-7.4.5.v20110725.jar
31 jetty-server-7.4.5.v20110725.jar
32 jetty-util-7.4.5.v20110725.jar
33 joda-time-1.6.2.jar
34 jra-1.0-alpha-4.jar
35 js-1.7R2.jar
36 jsr311-api-1.1.1.jar
37 msv-core-2010.2.jar
38 neethi-3.0.1.jar
39 opensaml-2.4.1.jar
40 openws-1.4.1.jar
41 relaxngDatatype-20020414.jar
42 saaj-api-1.3.jar
43 saaj-impl-1.3.2.jar
44 serializer-2.7.1.jar
45 slf4j-api-1.6.1.jar
46 slf4j-jdk14-1.6.1.jar
47 spring-aop-3.0.5.RELEASE.jar
48 spring-asm-3.0.5.RELEASE.jar
49 spring-beans-3.0.5.RELEASE.jar
50 spring-context-3.0.5.RELEASE.jar
51 spring-core-3.0.5.RELEASE.jar
52 spring-expression-3.0.5.RELEASE.jar
53 spring-jms-3.0.5.RELEASE.jar
54 spring-tx-3.0.5.RELEASE.jar
55 spring-web-3.0.5.RELEASE.jar
56 stax2-api-3.1.1.jar
57 velocity-1.7.jar
58 woodstox-core-asl-4.1.1.jar
59 wsdl4j-1.6.2.jar
60 wss4j-1.6.2.jar
61 xalan-2.7.1.jar
62 xml-resolver-1.2.jar
63 xmlbeans-2.4.0.jar
64 xmlschema-core-2.0.jar
65 xmlsec-1.4.5.jar
66 xmltooling-1.3.1.jar
67 xsdlib-2010.1.jar
View Code

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7 <!-- 两种方式配置1.监听器配置 2.servlet配置, 以下的是采用监听器配置的 -->
 8 
 9     <!-- 通过上下文参数指定spring配置文件的位置 -->
10     <context-param>
11         <param-name>contextConfigLocation</param-name>
12         <param-value>classpath:cxf-servlet.xml</param-value>
13     </context-param>
14     <listener>
15         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16     </listener>
17     
18     <!-- 配置CXF框架的核心Servlet  -->
19     <servlet>
20         <servlet-name>cxf</servlet-name>
21         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
22         <!-- 通过初始化参数指定配置文件的位置 -->
23         <!-- 
24         <init-param>
25             <param-name>config-location</param-name>
26             <param-value>classpath:cxf-servlet.xml</param-value>
27         </init-param>
28          -->
29     </servlet>
30 
31     <servlet-mapping>
32         <servlet-name>cxf</servlet-name>
33         <url-pattern>/cxf/*</url-pattern>
34     </servlet-mapping>
35     
36   <welcome-file-list>
37     <welcome-file>index.jsp</welcome-file>
38   </welcome-file-list>
39 </web-app>

cxf-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                         http://www.springframework.org/schema/beans/spring-beans.xsd
 7                         http://cxf.apache.org/bindings/soap 
 8                         http://cxf.apache.org/schemas/configuration/soap.xsd
 9                         http://cxf.apache.org/jaxws 
10                         http://cxf.apache.org/schemas/jaxws.xsd
11                         http://cxf.apache.org/jaxrs 
12                         http://cxf.apache.org/schemas/jaxrs.xsd
13                         ">
14     <!-- 引入CXF Bean定义如下,早期的版本中使用,如果是servlet引入的话则下面三句不用了,因为框架引入了 -->
15     <import resource="classpath:META-INF/cxf/cxf.xml" />
16     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
17     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
18 
19     <!-- 通过spring配置文件发布CXF的服务 -->
20 
21     <!-- 第一种发布方式:没有接口的发布(简单发布) -->
22     <!-- 
23         id:唯一标识
24         address:访问url
25         implementor:提供服务的类型 
26      -->
27     <jaxws:endpoint id="helloService" address="/hello"
28         implementor="cn.itcast.cxf.HelloService">
29         <!-- 加入消息拦截器 -->
30         <jaxws:inInterceptors>
31             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
32         </jaxws:inInterceptors>
33         <jaxws:outInterceptors>
34             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
35         </jaxws:outInterceptors>
36     </jaxws:endpoint>
37 
38     <!-- 第二种发布方式:带有接口的发布 -->
39     <!-- 
40         id:唯一标识
41         address:访问url
42         serviceClass:接口类型
43      -->
44     <jaxws:server id="hiService" address="/hi"
45         serviceClass="cn.itcast.cxf.IHiService">
46         <jaxws:serviceBean>
47             <!-- 提供服务的实现类 -->
48             <bean class="cn.itcast.cxf.HiServiceImpl"></bean>
49         </jaxws:serviceBean>
50 
51         <!-- 加入消息拦截器  -->
52         <jaxws:inInterceptors>
53             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
54         </jaxws:inInterceptors>
55         <jaxws:outInterceptors>
56             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
57         </jaxws:outInterceptors>
58     </jaxws:server>
59 
60     <!-- 配置restful方式的web服务 -->
61     <bean id="ps" class="cn.itcast.restful.PersonServiceImpl"></bean>
62     <jaxrs:server id="personService" address="/p">
63         <jaxrs:serviceBeans>
64             <ref bean="ps"/>
65         </jaxrs:serviceBeans>
66         <jaxrs:inInterceptors>
67             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
68         </jaxrs:inInterceptors>
69         <jaxrs:outInterceptors>
70             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
71         </jaxrs:outInterceptors>
72     </jaxrs:server>
73 </beans>

 

忽略60-72 那是 jason方面的配置

IHiService.java

bubuko.com,布布扣
1 package cn.itcast.cxf;
2 
3 import javax.jws.WebService;
4 
5 @WebService
6 public interface IHiService {
7     public String sayHi(String name);
8 }
View Code

HiServiceImpl.java

bubuko.com,布布扣
 1 package cn.itcast.cxf;
 2 
 3 public class HiServiceImpl implements IHiService {
 4 
 5     @Override
 6     public String sayHi(String name) {
 7         System.out.println("sayHi....");
 8         return "hi " + name;
 9     }
10 
11 }
View Code

然后

localhost:8080/项目地址/hi?xsdl

客户端

利用 wsimport -s 地址 或者ws2java -s地址的命令

得到文件后

把接口文件复制来

 

配置文件 ClientBean.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:soap="http://cxf.apache.org/bindings/soap"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                         http://www.springframework.org/schema/beans/spring-beans.xsd
 7                         http://cxf.apache.org/bindings/soap 
 8                         http://cxf.apache.org/schemas/configuration/soap.xsd
 9                         http://cxf.apache.org/jaxws 
10                         http://cxf.apache.org/schemas/jaxws.xsd">
11         <!-- 配置客户端bean -->
12         <!-- 
13             id:唯一标识
14             address:请求的服务地址
15             serviceClass:客户端接口
16          -->
17         <jaxws:client id="hiService" address="http://localhost/CXF_03/cxf/hi" serviceClass="cn.itcast.cxf.IHiService"></jaxws:client>
18         
19 </beans>

 

测试 IHiService就是拷贝过来的接口文件,放到项目中

 1 package cn.itcast.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import cn.itcast.cxf.IHiService;
 7 
 8 public class Test1 {
 9     public static void main(String[] args) {
10         //初始化spring
11         ApplicationContext ctx = new ClassPathXmlApplicationContext("ClientBeans.xml");
12         IHiService s = (IHiService) ctx.getBean("hiService");
13         s.sayHi("abc");
14         System.out.println(s.getClass().getName());
15     }
16 }

 

搭建web项目结合spring+cxf的webservice服务,布布扣,bubuko.com

搭建web项目结合spring+cxf的webservice服务

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/friends-wf/p/3806298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!