首页 > 代码库 > CXF WebService服务端成功(集成spring)
CXF WebService服务端成功(集成spring)
原文出自:http://blog.csdn.net/xuanjie25/article/details/7686118
http://liuzl121.iteye.com/blog/1733969
http://blog.sina.com.cn/s/blog_8ee5914d01016ctl.html
搭建webservice
由于做项目需要写一个手机客户端访问之前做好的项目,所以需要了解Apache CXF服务框架,调试了很久才把服务端跟测试的弄好,写下来免得以后忘记了
首先建一个web service project,然后把CXF相关的jar放到lib目录下,所需最少的jar包有:cxf-2.6.0.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jaxb-api-2.2.6.jar
jaxb-impl-2.2.5.jar
neethi-3.0.2.jar
wsdl4j-1.6.2.jar
xmlSchema-core-2.0.2.jar
官网下载地址:http://cxf.apache.org/download.html
因为我用到了Spring,所以还要加Spring的jar包,jar包加好后修改web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <display-name></display-name>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:applicationContext.xml
- classpath:cxf.xml
- </param-value>
- </context-param>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/webServices/*</url-pattern>
- </servlet-mapping>
- </web-app>
applicationContext.xml是Spring的配置文件没什么好说的,cxf.xml是配置CXF的,如下:
- <?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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.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-servlet.xml" />
- <jaxws:endpoint id="verificationService" implementor="VerificationServiceImpl" address="/verification" />
- </beans>
注意import的3句要加进去,是CXF的配置文件,在cxf-2.6.0.jar里能找到,不过我把这三句注释掉也能运行,测试也能通过,真奇怪···最后一句的address属性跟等下的访问地址有关,等下再说明,配置文件弄好了之后就要写服务接口了,
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface VerificationService {
- public String verification(@WebParam(name="securityCode") String securityCode);
- }
@WebhParam是接收客户端传过来的参数的,没有的话就不用写了,接口的实现类叫VerificationServiceImpl,在重写方法的时候要注意对null值的判断,不然会测试的时候会报错:Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Fault occurred while processing. 这样服务端就写好了,启动tomcat,输入网址:
http://localhost:8080/VTCManagerService/webServices/verification?wsdl,其中VTCManagerService是项目名,webService是在web.xml中配置的,而verification就是上面写到的address属性,正常的话就能看到一些xml文件,其中有个targetNamespace的属性就是客户端要访问服务端所需要的,不过在测试的时候用
不到,下面是测试部分:
为了方便我是在服务端直接写个类做测试的,如果是新建一个工程的话可以把服务端的工程导出为jar文件放在新建工程lib目录下,测试类代码:
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- import VerificationService;
- public class TestWebService {
- /**
- * @param args
- */
- public static void main(String[] args) {
- JaxWsProxyFactoryBean jfb = new JaxWsProxyFactoryBean();
- jfb.setServiceClass(VerificationService.class);
- jfb.setAddress
- ("http://localhost:8080/VTCManagerService/webServices/verification");
- VerificationService vs = (VerificationService) jfb.create();
- vs.verification("123456789");
- }
- }
客户端调用, cxf 调用webservice
标签:cxf调用webservice | 分类:cxfwebservice |
需要的jar上文章已说过。
服务器端发布的service地址为:http://localhost:8080/webP/webservice/helloWorld
1)在客户端先访问下,看能否访问到,在服务器发布的地址后面加上URL+?wsdl;如:http://localhost:8080/webP/webservice/helloWorld?wsdl访问。
如果看到xml文档信息,说明正常。
2)在dos下输入cmd运行,找到解压的apache-cxf-2.6.1的bin目录下, 运行
wsdl2java -p ws http://localhost:8080/webP/webservice/helloWorld?wsdl在apache-cxf-2.6.1的bin目录就生成了ws文件夹
如图:
3)把生成的ws文件夹中的java文件放到自己的项目中,这些java文件就是调用webservice所需要的类
4)运行测试类
import java.util.ArrayList;
import java.util.List;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import ws.HelloWorld;
import ws.User;
public class HelloWorldClient {
}
输出结果,over!
服务端配置另外一篇文章参考
用WebService很久了,但都是利用axis2去生成服务端掉别人的接口,重来没有自己写过,也不知道怎么去配置,今天利用点闲暇时间上网查了些资料,结合公司的项目自己做了个测试,我用的是CXF结合sping配置的服务端。需要用到的jar包具体我也不是很了解,网上说要用到一下这些,我没有去测试,拿过来了。
commons-logging-1.1.jar geronimo-activation_1.1_spec-1.0-M1.jar (or Sun‘s Activation jar) geronimo-annotation_1.0_spec-1.1.jar (JSR 250) geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun‘s JavaMail jar) geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun‘s Servlet jar) geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181) jaxb-api-2.1.jar jaxb-impl-2.1.6.jar jaxws-api-2.1.jar jetty-6.1.5.jar jetty-util-6.1.5.jar neethi-2.0.jar saaj-api-1.3.jar saaj-impl-1.3.jar stax-api-1.0.1.jar wsdl4j-1.6.1.jar wstx-asl-3.2.1.jar XmlSchema-1.2.jar xml-resolver-1.2.jar
The Spring jars (optional - for XML Configuration support):
aopalliance-1.0.jar spring-core-2.0.4.jar spring-beans-2.0.4.jar spring-context-2.0.4.jar spring-web-2.0.4.jar
And the CXF jar:
- cxf-2.1.jar
首先建立一个web项目,引入spring能力。
1. 建立webservice接口
- package com;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface TestI {
- String sayHello(@WebParam(name="word")String word);
- }
2. 建立接口的实现类
- package com;
- import javax.jws.WebService;
- @WebService(endpointInterface = "com.Test")
- public class Test implements TestI{
- public String sayHello(String word) {
- System.out.println("HELLO"+word);
- return "HELLO"+word;
- }
- }
3 .配置spring配置文件applicationContext.xml
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- <span style="background-color: #ff0000;">http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /></span>
- <jaxws:endpoint id="testWS"
- address="/test"
- implementorClass="com.TestI">
- <jaxws:implementor>
- <bean id="personDepotManagerImpl" class="com.Test"/>
- </jaxws:implementor>
- </jaxws:endpoint>
- </beans>
红色部分是需要加入的,下面的jaxws就是对webservice暴露的接口的定义。implementorClass是接口, jaxws:implementor中的bean是实现类
4 . 配置web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/classes/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <span style="background-color: #ff0000;"><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>/cxf/*</url-pattern>
- </servlet-mapping></span>
- </web-app>
红色部分是对webservice的配置,其中/cxf/*就是指webservice的访问路径了,比如我的项目叫CXF,我的访问路径就是http://127.0.0.1:8080/CXF/cxf这样就能看到所有的webservice。
CXF WebService服务端成功(集成spring)