GVKun编程网logo

在CXF开发的WebService接口WSDL信息中添加注释(cxf调用webservice接口)

10

这篇文章主要围绕在CXF开发的WebService接口WSDL信息中添加注释和cxf调用webservice接口展开,旨在为您提供一份详细的参考资料。我们将全面介绍在CXF开发的WebService接

这篇文章主要围绕在CXF开发的WebService接口WSDL信息中添加注释cxf调用webservice接口展开,旨在为您提供一份详细的参考资料。我们将全面介绍在CXF开发的WebService接口WSDL信息中添加注释的优缺点,解答cxf调用webservice接口的相关问题,同时也会为您带来cxf webservice接口 实现安全验证、CXF 根据已有的 WSDL 地址对接 WebService 接口、cxf 调用由axis2开发的webservice、cxf开发webservice的实用方法。

本文目录一览:

在CXF开发的WebService接口WSDL信息中添加注释(cxf调用webservice接口)

在CXF开发的WebService接口WSDL信息中添加注释(cxf调用webservice接口)

接口中使用注解定义

package edu.xxx.service;


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

import org.apache.cxf.annotations.WSDLDocumentation;
import org.apache.cxf.annotations.WSDLDocumentationCollection;

@WebService
@WSDLDocumentationCollection(  
	    {  
	        @WSDLDocumentation(value = "数据中心用户信息接口:为第三方子系统提供用户信息,访问此接口的客户端需要IP授权。作者:xxx,邮箱:xxx@126.com",  
	                           placement = WSDLDocumentation.Placement.TOP) 
	    }  
	)
public interface IUserService {
	@WebMethod  
	@WSDLDocumentation( "返回JSON格式数据") 
	String getUserListForJson(@WebParam(name="pageNum") Integer pageNum,
			@WebParam(name="pageSize") Integer pageSize);
	
	@WebMethod
	@WSDLDocumentation( "返回XML格式数据")
	String getUserListForXml(@WebParam(name="pageNum") Integer pageNum,
			@WebParam(name="pageSize") Integer pageSize);
	
	@WebMethod
	@WSDLDocumentation( "返回已经封闭好数据对象")
	ResponseObject getUserListForObject(@WebParam(name="pageNum") Integer pageNum,
			@WebParam(name="pageSize") Integer pageSize);
	
}
结果:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="IUserServiceService" targetNamespace="http://service.xxx.edu/">
  <wsdl:documentation>数据中心用户信息接口:为第三方子系统提供用户信息,需要访问此插口的客户端需要IP授权。作者:xxx,邮箱:xxx@126.com</wsdl:documentation>
  <wsdl:types>
    <xs:schema elementFormDefault="unqualified" targetNamespace="http://service.xxx.edu/" version="1.0">
      <xs:element name="getUserListForJson" type="tns:getUserListForJson"/>
      <xs:element name="getUserListForJsonResponse" type="tns:getUserListForJsonResponse"/>
      <xs:element name="getUserListForObject" type="tns:getUserListForObject"/>
      <xs:element name="getUserListForObjectResponse" type="tns:getUserListForObjectResponse"/>
      <xs:element name="getUserListForXml" type="tns:getUserListForXml"/>
      <xs:element name="getUserListForXmlResponse" type="tns:getUserListForXmlResponse"/>
      <xs:complexType name="getUserListForXml">
        <xs:sequence>
          <xs:element minOccurs="0" name="pageNum" type="xs:int"/>
          <xs:element minOccurs="0" name="pageSize" type="xs:int"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="getUserListForXmlResponse">
        <xs:sequence>
          <xs:element minOccurs="0" name="return" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="getUserListForJson">
        <xs:sequence>
          <xs:element minOccurs="0" name="pageNum" type="xs:int"/>
          <xs:element minOccurs="0" name="pageSize" type="xs:int"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="getUserListForJsonResponse">
        <xs:sequence>
          <xs:element minOccurs="0" name="return" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="getUserListForObject">
        <xs:sequence>
          <xs:element minOccurs="0" name="pageNum" type="xs:int"/>
          <xs:element minOccurs="0" name="pageSize" type="xs:int"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="getUserListForObjectResponse">
        <xs:sequence>
          <xs:element minOccurs="0" name="return" type="tns:responseObject"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="responseObject">
        <xs:sequence>
          <xs:element minOccurs="0" name="availablePage" type="xs:int"/>
          <xs:element maxOccurs="unbounded" minOccurs="0" name="data" nillable="true" type="tns:user"/>
          <xs:element minOccurs="0" name="message" type="xs:string"/>
          <xs:element minOccurs="0" name="status" type="xs:string"/>
          <xs:element minOccurs="0" name="totalRecords" type="xs:int"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="user">
        <xs:sequence>
          <xs:element minOccurs="0" name="userDept" type="xs:string"/>
          <xs:element minOccurs="0" name="userId" type="xs:string"/>
          <xs:element minOccurs="0" name="userIdentity" type="xs:string"/>
          <xs:element minOccurs="0" name="userName" type="xs:string"/>
          <xs:element minOccurs="0" name="userType" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
  </wsdl:types>
  <wsdl:message name="getUserListForObject">
    <wsdl:part element="tns:getUserListForObject" name="parameters"> </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserListForJson">
    <wsdl:part element="tns:getUserListForJson" name="parameters"> </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserListForObjectResponse">
    <wsdl:part element="tns:getUserListForObjectResponse" name="parameters"> </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserListForJsonResponse">
    <wsdl:part element="tns:getUserListForJsonResponse" name="parameters"> </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserListForXmlResponse">
    <wsdl:part element="tns:getUserListForXmlResponse" name="parameters"> </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserListForXml">
    <wsdl:part element="tns:getUserListForXml" name="parameters"> </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="IUserService">
    <wsdl:operation name="getUserListForXml">
      <wsdl:documentation>返回XML格式数据</wsdl:documentation>
      <wsdl:input message="tns:getUserListForXml" name="getUserListForXml"> </wsdl:input>
      <wsdl:output message="tns:getUserListForXmlResponse" name="getUserListForXmlResponse"> </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getUserListForJson">
      <wsdl:documentation>返回JSON格式数据</wsdl:documentation>
      <wsdl:input message="tns:getUserListForJson" name="getUserListForJson"> </wsdl:input>
      <wsdl:output message="tns:getUserListForJsonResponse" name="getUserListForJsonResponse"> </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getUserListForObject">
      <wsdl:documentation>返回已经封闭好数据对象</wsdl:documentation>
      <wsdl:input message="tns:getUserListForObject" name="getUserListForObject"> </wsdl:input>
      <wsdl:output message="tns:getUserListForObjectResponse" name="getUserListForObjectResponse"> </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="IUserServiceServiceSoapBinding" type="tns:IUserService">
    <soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getUserListForJson">
      <soap:operation soapAction=""/>
      <wsdl:input name="getUserListForJson">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getUserListForJsonResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getUserListForXml">
      <soap:operation soapAction=""/>
      <wsdl:input name="getUserListForXml">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getUserListForXmlResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getUserListForObject">
      <soap:operation soapAction=""/>
      <wsdl:input name="getUserListForObject">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getUserListForObjectResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="IUserServiceService">
    <wsdl:port binding="tns:IUserServiceServiceSoapBinding" name="IUserServicePort">
      <soap:address location="http://localhost:8080/UserSAPI/service/user"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>



cxf webservice接口 实现安全验证

cxf webservice接口 实现安全验证

@H_301_4@

 

   基于 spring + apache cxf

 

1.服务器端 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"

xmlns:jaxrs="http://cxf.apache.org/jaxrs"

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

 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

 

    <import resource="classpath:meta-inf/cxf/cxf.xml" />  

    <import resource="classpath:meta-inf/cxf/cxf-servlet.xml" />  

    

<jaxws:endpoint id="greetingService"

implementor="com.test.service.impl.GreetingServiceImpl" 

address="/greetingService" >

<jaxws:inInterceptors>   

            <bean   

               />   

            <bean   

               https://www.jb51.cc/tag/so/" target="_blank">.soap.saaj.saajInInterceptor" />   

            <bean   

               https://www.jb51.cc/tag/ecurity/" target="_blank">ecurity.wss4j.WSS4JInInterceptor">   

                <constructor-arg>   

                    <map>   

                        <entry key="action" value="Usernametoken" />   

                        <entry key="passwordType"  

                            value="PasswordText" />   

                        <entry key="user" value="cxfUser" />   

                        <entry key="passwordCallbackRef">   

                            <ref bean="serverPasswordCallback" />   

                        </entry>   

                    </map>   

                </constructor-arg>   

            </bean>   

        </jaxws:inInterceptors>   

    </jaxws:endpoint>

<bean id="serverPasswordCallback"  

       />   

</beans> 

说明:

   action:Usernametoken指使用用户令牌 
    passwordType:PasswordText指密码加密策略,这里直接文本 
    user:cxfServer指别名 
    passwordCallBackRef:serverPasswordCallback指消息验证 

 

2.服务器端 CallbackHandler 拦截处理

package com.core.common.cxf;

 

import java.io.IOException;   

  

import javax.security.auth.callback.Callback;   

import javax.security.auth.callback.CallbackHandler;   

import javax.security.auth.callback.UnsupportedCallbackException;   

import org.apache.wss4j.common.ext.WSPasswordCallback;

public class ServerPasswordCallback implements CallbackHandler {   

    public void handle(Callback[] callbacks) throws IOException,  

            UnsupportedCallbackException {   

        WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];   

        String pw = pc.getpassword();   

        String idf = pc.getIdentifier();   

        System.out.println("password:"+pw);   

        System.out.println("identifier:"+idf);   

        if (idf.equals("admin")) {   

        pc.setPassword("josen");

        } else {   

            throw new SecurityException("验证失败");   

        }   

    }   

  

 

}  

说明:

 

  WSPasswordCallback 的passwordType属性和password 属性都为null,你只能获得用户名(identifier),一般这里的逻辑是使用这个用户名到数据库中查询其密码,然后再设置到password 属性,WSS4J 会自动比较客户端传来的值和你设置的这个值。你可能会问为什么这里CXF 不把客户端提交的密码传入让我们在ServerPasswordCallbackHandler 中比较呢?这是因为客户端提交过来的密码在SOAP 消息中已经被加密为MD5 的字符串,如果我们要在回调方法中作比较,那么第一步要做的就是把服务端准备好的密码加密为MD5 字符串,由于MD5 算法参数不同结果也会有差别,另外,这样的工作CXF 替我们完成不是更简单吗?

 

根据上面说的,我获取的password 为null,所以这里就不用自己判断密码了,只要验证用户名后,在设置密码就可以自动验证了

3.客户端配置文件:

<?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"

xmlns:jaxrs="http://cxf.apache.org/jaxrs"

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

 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

 

  <jaxws:client id="greetingService" service 

  address="http://localhost:8080/test/greetingService">

 

          <jaxws:outInterceptors>   

            <bean   

               />   

            <bean   

               https://www.jb51.cc/tag/so/" target="_blank">.soap.saaj.saajOutInterceptor" />   

            <bean   

               https://www.jb51.cc/tag/ecurity/" target="_blank">ecurity.wss4j.WSS4JOutInterceptor">   

                <constructor-arg>   

                    <map>   

                        <entry key="action" value="Usernametoken" />   

                        <entry key="passwordType"  

                            value="PasswordText" />   

                        <entry key="user" value="cxfUser" />   

                        <entry key="passwordCallbackRef">   

                            <ref bean="clientPasswordCallback" />   

                        </entry>   

                    </map>   

                </constructor-arg>   

            </bean>   

        </jaxws:outInterceptors> 

    </jaxws:client>   

    <bean id="clientPasswordCallback"  

       />   

 

</beans> 

4.客户端CallbackHandler

package com.common.cxf;

 

import java.io.IOException;   

 

import javax.security.auth.callback.Callback;   

import javax.security.auth.callback.CallbackHandler;   

import javax.security.auth.callback.UnsupportedCallbackException;   

 

import org.apache.wss4j.common.ext.WSPasswordCallback;

  

 

  

public class ClientPasswordCallback implements CallbackHandler {   

  

    public void handle(Callback[] callbacks) throws IOException,  

            UnsupportedCallbackException {   

        for(int i=0;i<callbacks.length;i++)   

        {   

             WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];   

             pc.setPassword("josen");   

             pc.setIdentifier("admin");   

        }   

    }   

  

 

}  

    说明:

  只要两边的 Identifier,Password 相同就ok了

CXF 根据已有的 WSDL 地址对接 WebService 接口

CXF 根据已有的 WSDL 地址对接 WebService 接口

1.WSDL地址:http://ip:8080/Service/?wsdl

2.下载cxf ;cmd下跳转到你想生成到那个目录的具体位置;然后用 cxf 的命令 wsdl2java 生成对应的:C:\Users\Administrator\Desktop\sms>wsdl2java http://ip:8080/Service/?wsdl

3.导入CXF需要的jar包经测试最少需要这些包(我用的是CXF3.1.1):

asm-5.0.4.jar
cxf-core-3.1.1.jar
cxf-rt-bindings-soap-3.1.1.jar
cxf-rt-bindings-xml-3.1.1.jar
cxf-rt-databinding-jaxb-3.1.1.jar
cxf-rt-frontend-jaxws-3.1.1.jar
cxf-rt-frontend-simple-3.1.1.jar
cxf-rt-transports-http-3.1.1.jar
cxf-rt-ws-addr-3.1.1.jar
cxf-rt-ws-policy-3.1.1.jar
cxf-rt-wsdl-3.1.1.jar
jaxb-core-2.2.11.jar
jaxb-impl-2.2.11.jar
neethi-3.0.3.jar
stax2-api-3.1.4.jar
woodstox-core-asl-4.4.1.jar
wsdl4j-1.6.3.jar
xml-resolver-1.2.jar
xmlschema-core-2.2.1.jar

4.根据WebService对外暴漏的接口或者类些对应的代码

import org.apache.cxf.jaxws.JaxWsProxyfactorybean;
import org.tempuri.IService;


public class SendMessage {
public static void main(String[] args) {
        JaxWsProxyfactorybean svr = new JaxWsProxyfactorybean();  
        svr.setServiceClass(IService.class);  
        svr.setAddress("http://ip:8080/Service/?wsdl");  
        IService s = (IService) svr.create();//IService 为暴漏的接口
        String submitSms = s.submitSms("username","password");//submitSms为需要调用的对应的方法
             System.out.println(submitSms);
        
}
}

至此就完成了。以备后用。

http://download.csdn.net/download/lg906321/8811027  这个链接是JAR的打好的包;不好意思,不会搞链接就下载的需要的就多点下了谢谢!

cxf 调用由axis2开发的webservice

cxf 调用由axis2开发的webservice

public class MainClient { public static void main(String args[]){ Listservice service = new Listservice(); ListservicePortType port = service.getListserviceHttpSoap11Endpoint(); // ListservicePortType port = service.getListserviceHttpSoap12Endpoint(); List<String> list = port.getList(); for(String str : list){ System.out.println(str); } } }

cxf开发webservice

cxf开发webservice

本文也是参照网上的教程,自己写的cxf的两个小例子,一个是普通的java application,一个是和spring整合的web application,想通过两者的对比进行更加深入的理解

项目地址: http://download.csdn.net/detail/u012049463/6722275



服务端:1. 先定义一个服务接口,然后实现这个借口,最后进行发布。

代码如下:

User.java,定义一个服务需要的对象:

package com.unj.service2;

public class User {
	int id;
	String name;
	String address;
	
	//get、set方法
	@Override
	public String toString() {
		return "User [id=" + id + ",name=" + name + ",address=" + address
				+ "]";
	}

}

CxfService.java,服务接口,这里值得注意的就是几口上面的@webservice注解:

package com.unj.service2;

import javax.jws.WebService;

@WebService
public interface CxfService {
	public User getUser(int id);
	public boolean addUser(User user);
}

CxfServiceImpl.java, 服务实现:

package com.unj.service2;

import javax.jws.WebService;

@WebService
public class CxfServiceImpl implements CxfService {
	@Override
	public User getUser(int id) {
		User user = new User(id,"unj","wuhan");
		return user;
	}
	@Override
	public boolean addUser(User user) {
		try {
			if (Integer.valueOf(user.getId()) != null) {
				return true;
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		return false;
	}
}

CxfPublish.java, 对服务进行发布:

package com.unj.service2;

import java.io.IOException;

import javax.xml.ws.Endpoint;

public class CxfPublish {
	public static void main(String[] args) throws IOException {
		CxfServiceImpl cws = new CxfServiceImpl();
		Endpoint.publish("http://localhost:1234/cxfWebservice",cws);
	}
}

然后通过浏览器,输入“http://localhost:1234/cxfWebservice?wsdl” 就可以看到我们发布的websense的xml内容。


下面编写客户端调用我们之前写的webservice。


首先我们得下载cxf然后安装,下载地址:http://www.apache.org/dyn/closer.cgi?path=%2Fcxf%2F2.3.1%2Fapache-cxf-2.3.1.zip    下载之后其实并没有安装文件,我们需要解压缩自后进行环境变量的配置,假如我们将cxf zip文件解压到了D:/javaApp目录下,我们需要配置的环境变量如下:

在CLAsspATH中添加 D:\JavaApp\apache-cxf-2.7.5\lib;

新建CXF_HOME D:\JavaApp\apache-cxf-2.7.5;

在PATH中添加 D:\JavaApp\apache-cxf-2.7.5\bin

配置好之后,我们cmd进入到bin目录中,运行wsdl2java http://localhost:1234/cxfWebservice?wsdl,运行之后会生成一个文件夹,(上面是根据web url进行转化,还有种方法是根据wsdl文件进行转化,如test.wsdl,转化命令为:

wsdl2java -p com.unj -d d:/abc -all  test.wsdl
-p  指定其wsdl的命名空间,也就是要生成代码的包名

-d  指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant  生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,service interface,server mainline,client mainline,implementation object,and an Ant build.xml file.

),该文夹跟包名相同,然后将代码拷到项目中(如上图所示),将生产的代码拷贝到项目中,其中有一个类会报错,将报错的几行代码注释起来(我不明白为什么会报错),然后再继续下面的步骤,之后,我们就可以像使用本地类一样使用该类中的方法,代码如下:

package com.unj.client2;

import com.unj.service2.CxfServiceImpl;
import com.unj.service2.CxfServiceImplService;
import com.unj.service2.User;
public class Client {
	public static void main(String[] args) {
		CxfServiceImplService impl = new CxfServiceImplService();
		CxfServiceImpl server = impl.getCxfServiceImplPort();
		User user = new User();
		user.setId(001);
		boolean seaved = server.addUser(user);
		System.out.println(seaved);
	}
}

以上是通过javaapplication来开发和调用的webservice,下面我们再来看看spring和cxf整合起来的webService,这个项目是用maven开发,所以在导入到自己的eclipse时注意区别。


步骤: 1. 定义接口  2.实现接口  3.配置cxf-beans  4. 配置web.xml

首先要通过maven导入jar包: cxf-rt-frontend-jaxws, cxf-rt-transports-http,spring-context,spring-web;


cxfService.java:

package com.unj.service2;

import javax.jws.WebService;

@WebService
public interface CxfService {
	public User getUser(int id);
	public boolean addUser(User user);
}

CxfServiceImpl.java:

package com.unj.service2;

import javax.jws.WebService;

@WebService
public class CxfServiceImpl implements CxfService {

	@Override
	public User getUser(int id) {
		User user = new User(id,"wuhan");
		return user;
	}

	@Override
	public boolean addUser(User user) {
		try {
			if (Integer.valueOf(user.getId()) != null) {
				return true;
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		return false;
	}

}

cxf-beans.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:jaxws="http://cxf.apache.org/jaxws"
	xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
	default-autowire="byType" default-lazy-init="true">

	<description>使用Apache CXF的Web Service配置文件,以下三个为固定配置文件(不需要创建)
	</description>
	<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" />

	<!--在这里配置相应内容 -->
	<!--id:随意配,implementor:指定接口具体实现类,address:随意配,访问时会用到,下面会做说明 -->
	<jaxws:endpoint id="userService" implementor="com.unj.service2.CxfServiceImpl"
		address="/userService">
	</jaxws:endpoint>
</beans>

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Archetype Created Web Application</display-name>


	<!--cxf start -->
	<!--用于加载cxf-beans.xml配置信息 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/cxf-beans.xml</param-value>
	</context-param>
	<!--使用spring ContextLoaderListener 加载cxf-beans.xml -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--配置CXFServlet -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<display-name>CXF Servlet</display-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可自定义配置,用于CXFServlet请求地址拦截,访问会用到 -->
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	<!--cxf end -->

</web-app>
 

然后启动项目,在浏览器输入“http://localhost:8080/cxfWSTest/services/userService?wsdl”就可以看见我们发布的webservice的wsdl。

服务端发布后,我们再来看看客户端的实现。


步骤:1. 通过cxf将wsdl转化为java代码并拷贝到项目中, 2. 配置beans.xml  

进入到cxf的bin目录,运行wsdl2java  http://localhost:8080/cxfWSTest/services/userService?wsdl ,然后将生成的代码拷贝进项目。

cxfClient.java:

package com.unj.invoke;

import org.springframework.context.support.ClasspathXmlApplicationContext;

import com.unj.service2.CxfService;
import com.unj.service2.User;

public class CxfClient {
	public static void main(String[] args) {
		ClasspathXmlApplicationContext app = new ClasspathXmlApplicationContext("beans.xml");//此处beans.xml放在src下,也需要放在其他目录下,但需要注明清楚
		//获取webservice服务的操作接口
		CxfService userService = (CxfService)app.getBean("userServeiceClient");
		User user = userService.getUser(1);
		System.out.println(user);
	}
}

这样就实现了cxf与spring的整合。



另外附上cxf客户端调用webservice的几种方式:http://www.voidcn.com/article/p-bigjzjgt-bbc.html

public static void invokeMethod1() {
  long s=new Date().getTime();
  UserServiceImplService serivce = new UserServiceImplService();
  UserServiceImpl impl = serivce.getUserServiceImplPort();
  User u = new User();
  impl.addUser(u);
  long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用1297
  //1437 1062 1063 1078 1047
 }

 public static void invokeMethod2() {
  long s=new Date().getTime();
  JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();
  factory.setAddress("http://localhost:8088/abc");
   QName SERVICE = new QName("http://liaomin","UserServiceImplService");
   factory.setServiceName(SERVICE);
  factory.setServiceClass(UserService.class);
  UserService us = (UserService) factory.create();
  User u = new User();
  // us.addUser(u);
  us.addUser(u);
  long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用1265
  //1047 1047(比较稳定)
 }

 public static void invokeMethod3() throws MalformedURLException {
  long s=new Date().getTime();
     QName SERVICE = new QName("      QName UserServiceImplPort = new QName("   URL url = new URL("http://localhost:8088/abc?wsdl");
  ServiceDelegate dele=Provider.provider().createServiceDelegate(url,SERVICE,Service.class);
  UserService us = (UserService) dele.getPort(UserServiceImplPort,UserService.class);
  User u = new User();
  us.addUser(u);
  long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用 1281 
  //1047 1031 1047 1016 1032
 }
 public static void invokeMethod4()
 {
  long s=new Date().getTime();
  ClientProxyfactorybean factory = new ClientProxyfactorybean();
        factory.setServiceClass(UserService.class);
        factory.setAddress("http://localhost:8088/abc");
       // factory.getServiceFactory().setDataBinding(new AegisDatabinding());
        UserService client = (UserService) factory.create();
        User u = new User();
        client.addUser(u);
        long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用 1188 
  //调用一次后   1016 1000 1016 1015
 }

 

通过实践去测试 只有第四种调用耗费的时间最少的

如果在频繁取数据的系统中 能优化100ms的速度 对性能就会有大大的提高

比如我调用 100次webservice发送消息  每发送一次 是 1200ms(1.2s) 总共耗时 120s

如果采用第四种 只需要 1000ms(1s) 总共耗时 100s  那么优化了20s时间 20s是个什么概念

假如是调用 1000 10000次发送了  优化的效率就更大

关于在CXF开发的WebService接口WSDL信息中添加注释cxf调用webservice接口的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于cxf webservice接口 实现安全验证、CXF 根据已有的 WSDL 地址对接 WebService 接口、cxf 调用由axis2开发的webservice、cxf开发webservice的相关信息,请在本站寻找。

本文标签: