加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用CXF简单开发webservice的实例

发布时间:2020-12-17 00:03:35 所属栏目:安全 来源:网络整理
导读:1、建立一个web Project,导入相应的jar包 点击下载cxf的jar包 2、定义将要被暴露成webservice的接口 package com.xzb.demo.interfaces;import javax.jws.WebService;@WebServicepublic interface HelloWorld {public String sayHello(String name);} 3、定义

1、建立一个web Project,导入相应的jar包

点击下载cxf的jar包

2、定义将要被暴露成webservice的接口

 
package com.xzb.demo.interfaces;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
	public String sayHello(String name);
}

3、定义实现接口的Impl

package com.xzb.demo.impl;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import com.xzb.demo.interfaces.HelloWorld;

@WebService
@SOAPBinding(style=Style.RPC)
public class HelloWorldImpl implements HelloWorld {

	public String sayHello(@WebParam(name="name")String name) {
		return name+" say:Hello world!";
	}
}

4、发布及访问web Service

(1)代码实现

发布web Service:

package com.xzb.demo.serviceApp;

import javax.xml.ws.Endpoint;
import com.xzb.demo.impl.HelloWorldImpl;

public class WebServiceApp {
	public static void main(String[] args) {
		 System.out.println("web service start");
         HelloWorldImpl implementor= new HelloWorldImpl();
         String address="http://localhost:80/helloWorld";
         Endpoint.publish(address,implementor);
         System.out.println("web service started");
	}

}

运行以上程序后,访问地址http://localhost/helloWorld?wsdl,即可查看到对应的wsdl。

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:ns1="http://interfaces.demo.xzb.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.demo.xzb.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldImplService" targetNamespace="http://impl.demo.xzb.com/">
<wsdl:import location="http://localhost/helloWorld?wsdl=HelloWorld.wsdl" namespace="http://interfaces.demo.xzb.com/"></wsdl:import>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="ns1:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldImplService">
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldPort">
<soap:address location="http://localhost:80/helloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
备注:虽然在Impl中采用注释的方式标识接口方法所需参数@WebParam(name="name"),但不起作用。试了之后,发现如果接口不采用@WebService,只在接口实现类上采用@WebService,@WebParam(name="name"),在wsdl上即可看到所需参数和返回值等信息。


客户端访问web Service:

package com.xzb.demo.serviceClient;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.xzb.demo.interfaces.HelloWorld;

public class HelloWorlClient {
	public static void main(String[] args) throws Exception {
		
	     JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
         svr.setServiceClass(HelloWorld.class);
         svr.setAddress("http://localhost:80/helloWorld");
         HelloWorld hw = (HelloWorld) svr.create();
         System.out.println(hw.sayHello("xzb"));
		
	}

}

client运行结果:

2013-10-18 10:38:49 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://interfaces.demo.xzb.com/}HelloWorldService from class com.xzb.demo.interfaces.HelloWorld
xzb say:Hello world!

(2)与spring结合实现

在spring配置文件上发布webservice和客户端访问webservice的配置。配置代码如下:

<?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"/>
 
                 <!--在项目的  /helloWorld   路径将 com.xzb.demo.impl.HelloWorldImpl 暴露出去-->
                 <jaxws:endpoint 
                              id="helloWorld"
                              implementor="com.xzb.demo.impl.HelloWorldImpl"
                              address="/helloWorld"  />
   
                <!-- 客户端访问webservice配置 -->
                <bean id="client" class="com.xzb.demo.interfaces.HelloWorld" 
                           factory-bean="clientFactory" factory-method="create"/>
    
                 <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
                            <property name="serviceClass" value="com.xzb.demo.interfaces.HelloWorld"/>
                            <property name="address" value="http://localhost:80/CXFWebServiceTest/webservice/helloWorld"/>
                 </bean>
                
                 
     </beans>

然后,访问http://localhost/CXFWebServiceTest/webservice/helloWorld?wsdl可以查看wsdl,client只要通过访问http://localhost:80/CXFWebServiceTest/webservice/helloWorld,就可以调用到接口com.xzb.demo.interfaces.HelloWorld提供的方法。

package com.xzb.demo.serviceClient;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xzb.demo.interfaces.HelloWorld;

public class HelloWorlClient {
	public static void main(String[] args) throws Exception {
		
		  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		  HelloWorld client = (HelloWorld)context.getBean("client");
		  System.out.println(client.sayHello("xzb"));
		
	}

}





初步接触cxf,尚有很多不清楚的地方,如果错误,请指正。(以上client访问webservice,必须依赖于service端的接口,才能调用到正确的方法,但感觉从wsdl上得出信息貌似不太容易,因为目前还不太熟悉。)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读