webservice+cxf+spring

今天主要讲3个方面的内容:

      目录:

             1:通过spring_cxf创建webservice。

              2:通过CXF的wsdl2java创建java类。

              3:客户端调用。

1:创建webservice

     首先创建个maven-archetype-webapp项目,不会创建参考我博客spring_ssh.

     其次在web.xml中添加spring容器监听配置和cxf配置。代码如下

     

<?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"  
    id="WebApp_ID" version="2.5">  
    <display-name>webservice</display-name>  
	<!-- springmvc 上下文监听器 ContextLoaderListener -->
	<listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/*.xml</param-value>
	</context-param>
	<!-- springmvc 上下文监听器  ContextLoaderListener-->
    <servlet>  
        <servlet-name>CXFService</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>CXFService</servlet-name>  
        <url-pattern>/ws/*</url-pattern>  
    </servlet-mapping>  
</web-app>

 然后是配置spring.xml,spring中主要配置jaxws:endpoint,代码如下:

<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">
	 <bean id="bookServiceImpl" class="com.xiaoji.webservice.service.impl.BookServiceImpl"></bean>  	
		
    <jaxws:endpoint id="bookService" implementor="#bookServiceImpl" address="/bookService">
        <jaxws:properties>
            <entry key="schema-validation-enabled" value="true"/>
        </jaxws:properties>
        <jaxws:dataBinding>
            <bean class="org.apache.cxf.xmlbeans.XmlBeansDataBinding"/>
        </jaxws:dataBinding>
    </jaxws:endpoint>
</beans>

 

 

注解:jaxws:endpoint属性介绍,implementor是webservice接口实现类,address是wsdl访问路径地址还有一个是本地生成wsdl文件:wsdlLocation="wsdl/hello_world.wsdl"。

最后是添加webservice类和接口:

BookService.java

 

package com.xiaoji.webservice.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface BookService {

	//http://127.0.0.1:8080/webservice/ws/bookService?wsdl
	
	@WebMethod(operationName = "helloWebService")  
	@WebResult(name = "result")  
	public String helloWebService(@WebParam(name = "ws") String ws);
}

 BookServiceImpl.java

 

 

package com.xiaoji.webservice.service.impl;

import javax.jws.WebService;

import com.xiaoji.webservice.service.BookService;

@WebService(endpointInterface = "com.xiaoji.webservice.service.BookService")  
public class BookServiceImpl implements BookService {

	public String helloWebService(String ws) {
		// TODO Auto-generated method stub
		return "hello webservice ,this is my first webservice. this is " + ws;
	}

}

 最后当然不能缺少pom.xml源码哦:

 

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xiaoji.ssh</groupId>
  <artifactId>webservice</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>webservice Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
   <properties>
  	<spring.version>3.2.8.RELEASE</spring.version>	
  	<jdk.version>1.6</jdk.version>
  </properties>
  <dependencies>
         <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-databinding-xmlbeans</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- Jetty is needed if you're using the CXFServlet -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
  </dependencies>
 
  <build>
    	<plugins>
	  	<plugin>
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-compiler-plugin</artifactId>
	        <version>3.1</version>
	        <configuration>
	          <source>${jdk.version}</source>
	          <target>${jdk.version}</target>
	        </configuration>
	      </plugin>
  	</plugins>
    <finalName>webservice</finalName>
  </build>
</project>

 大功告成,运行jetty,访问地址:http://172.16.141.55:8080/webservice/ws/bookService?wsdl,改成自己机子IP,OK.webservice-cxf-service就创建完成了

 

2:webservice的服务器端创建了,如何建立测试端呢,我查阅了很多资料,翻看了cxf源码,发现cxf里面有一个wsdl2java.bat,这是个好工具啊,可以根据wsdl链接地址生成客户端访问程序。当然最后也会把cxf-3.0.zip包共享出来。这节主要讲wsdl工具的使用:

(1):解压apache-cxf-3.0.0.zip,配置环境变量,CXF_HOME=cxf的解压路径,path添加%CXF_HOME%\bin,新建CLASS_PATH=.;%CXF_HOME%\lib,有CLASS_PATH的朋友直接添加%CXF_HOME%\lib,最后运行cmd,wsdl2java -v,显示出版本信息,配置成功,如下。

C:\Users\Administrator>wsdl2java -v

wsdl2java - Apache CXF 3.0.0

 (2):运行wsdl2java -p com.xiaoji.webservice.service -d E:\Lynch\De

velop\workspace\eclipse4.3.0\webservicecxf\src\main\java -client http://172.16.1

41.55:8080/webservice/ws/bookService?wsdl,这里朋友们对wsdl2java 后面的参数肯定会有疑问

                wsdl -p 打包 -d 生成JAVA文件存放路径 -client 是wsdl访问路径

这里要注意一点打包,是头文件里面ns1后面跟的链接反向就OK如: xmlns:ns1="http://service.webservice.xiaoji.com/;还有一点是java文件存放路径是我新建的一个maven-simple-project下的src/main/java下的路径,一切都准备就绪,在eclipse刷新客户端项目,会出现如下目录文件:



 新建一个WebserviceCxfClient复制BookService_BookServiceImplPort_Client类中的代码运行,也可以直接运行
BookService_BookServiceImplPort_Client,输出结果,淡然我个人觉的这个客户端类写的太复杂了,因为他用到main输入参数我简化了哈,代码如下:

 

package com.xiaoji.webservice.cxf.client;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;

import com.xiaoji.webservice.service.BookService;
import com.xiaoji.webservice.service.BookServiceImplService;

public class WebserviceCxfClient {
	private static final QName SERVICE_NAME = new QName("http://impl.service.webservice.xiaoji.com/", "BookServiceImplService");
	public static void main(String[] args) throws MalformedURLException {
		URL wsdlURL = new URL("http://172.16.141.55:8080/webservice/ws/bookService?wsdl");
        
        BookServiceImplService ss = new BookServiceImplService(wsdlURL, SERVICE_NAME);
        BookService bs = ss.getBookServiceImplPort();  
        
        {
        System.out.println("Invoking helloWebService...");
        String ws = "小吉";
        String result = bs.helloWebService(ws);
        System.out.println("helloWebService.result=" + result);
        }

        System.exit(0);
	}

}

客户端pom.xml

 

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xiaoji.webservice.cxf.client</groupId>
  <artifactId>webservicecxf</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>webservice-cxf-client</name>
  
  <dependencies>
	<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-databinding-xmlbeans</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.0.0</version>
        </dependency>
  </dependencies>
</project>

  

 

这样才符合自己的风格呵呵,下面我就上传源代码,为什么要先上传cxf呢,因为xfire好久没更新了,官网也很乱不好找资料。呵呵。

客户端也可以和spring通用哈,我这里只给出spring.xml的配置大家自己下来练习哈,

spring-client.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" 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">
    <jaxws:client xmlns:s="http://apache.org/hello_world_soap_http" id="client" serviceClass="org.apache.hello_world_soap_http.Greeter" serviceName="s:SOAPService" endpointName="s:SoapPort" wsdlLocation="wsdl/hello_world.wsdl" address="http://localhost:9000/SoapContext/SoapPort">
        <jaxws:properties>
            <entry key="schema-validation-enabled" value="true"/>
        </jaxws:properties>
        <jaxws:dataBinding>
            <bean class="org.apache.cxf.xmlbeans.XmlBeansDataBinding"/>
        </jaxws:dataBinding>
    </jaxws:client>
</beans>

 大家可以自己尝试哈,这里就不详细说了,感觉也没太大的难度了。

 

上传附件失败,只有给大家云盘地址了,如果该文章对你有帮助的话,回复鼓励下作者以后才有动力写相同类似文章。客户端和服务器源码已经上传,apache-cxf-3.0.0.zip云盘下载地址为:http://pan.baidu.com/s/1eQoZFsI,官网下载地址:

http://www.apache.org/dyn/closer.cgi?path=/cxf/3.0.0/apache-cxf-3.0.0.zip

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值