webservice 整合spring 使用接口发布服务

               webservice 虽不是必须的技术,但却是必不可少的技术。那么在实际项目中改如何运用webservice呢。

              下面使用比较流行的webservice框架CXF来整合spring进行开发

              

             第一步导入jar包

           

          第二步编写服务端的接口

            

package kewei.webserver.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


@WebService
public interface IOrderService {
	
	List<OrderStep> getOrderStepListByOrderId(String orderId);
	
	
}

             接口实现类

package kewei.webserver.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



public class IOrderServiceImpl implements IOrderService{
	
	@Override
	public List<OrderStep> getOrderStepListByOrderId(@WebParam(name="orderId")String orderId) {
		ArrayList<OrderStep> osList=new ArrayList<OrderStep>();
		OrderStep os1=new OrderStep();
		os1.setStepMessage("开始");
		os1.setStepDateStr("2014-03-24");
		
		OrderStep os2=new OrderStep();
		os2.setStepMessage("结束");
		os2.setStepDateStr("2014-03-24");
		osList.add(os1);
		osList.add(os2);
		System.out.println("获取订单跟踪信息成功!!!");
		return osList;
	}

}

第三步,编写spring-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"
    xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.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:server id="orderWebService" address="/order" serviceClass="kewei.webserver.order.IOrderService">
        <jaxws:serviceBean>
            <bean class="kewei.webserver.order.IOrderServiceImpl"></bean>
        </jaxws:serviceBean>
        <jaxws:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
    </jaxws:server>
</beans>

然后再服务器启动的时候加载spring-cxf配置文件。这个我就不贴了,只要是会用spring的人应该都会


最后一步,测试下,看服务是否发布成功。那么服务的发布就告一段落了。


接下来介绍调用服务的三种方式


方式一:cxf整合spring框架进行调用

       适用场景

           这种方式适合于拥有spring环境,且需要将服务接口注入到spring框架中

 

操作步骤

1,导入jar包(和服务端的jar一致)

       

2,使用wsdl2java 工具或者wsimport工具生成java代码

                    wsdl2java-pkewei.webserver.orderhttp://192.168.6.89:8080/custom/cxf/order/IOrderService?wsdl

               

3,编写spring-cxf.xml配置文件

<?xmlversion="1.0" encoding="UTF-8"?>

<beansxmlns="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"

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

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd

http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd

http://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsd">

<importresource="classpath:META-INF/cxf/cxf.xml" />

<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

<importresource="classpath:META-INF/cxf/cxf-servlet.xml" />

<!-- id:唯一标示 address:服务的地址 serviceClass:客户端生成的接口 -->

<jaxws:client id="iorderService"address="http://192.168.6.89:8080/custom/cxf/order/IOrderService"serviceClass="kewei.webserver.order.IOrderService"

</jaxws:client>

</beans>


 4,在项目启动的时候加载spring-cxf.xml

5,注入iorderService 调用方法

 

方式二:使用jquery ajax调用web服务

              适用场景

                    适用于非java环境,可以跨平台调用

              操作步骤

                    1, 可参见示例(使用jquery进行访问,需解决jquery的跨域访问问题)

                              

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index.html</title>
	<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
	<script type="text/javascript">
		function sendJqueryAjax(){
						var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://order.webserver.kewei/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
+'<soapenv:Body><q0:getOrderStepListByOrderId><arg0>123</arg0></q0:getOrderStepListByOrderId></soapenv:Body></soapenv:Envelope>';
			$.ajax({
					url:'http://192.168.6.89:8090/custom/cxf/order',
					type:'post',
					dataType:'xml',
					contentType:'text/xml;charset=UTF-8',
					data:soap,
             	    success:function(data,status,xhr){
             	    	
             	    	alert($(data).find('return').children().eq(0).text());
             	    	
             	    },
             	    error:function(){
             	    	alert('errro!!');
             	    }
					});
		}
	</script>

  </head>
  
  <body>
    <input type="button" οnclick="sendJqueryAjax();" value="通过jqery调用webservice服务">
  </body>
</html>

                    2, 响应体参数解释

                   <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

                      <soap:Body><ns2:getOrderStepListByOrderIdResponsexmlns:ns2="http://order.webserver.kewei/">

                     <return><stepDateStr>2014-03-24</stepDateStr><stepMessage>开始</stepMessage></return>

                      <return><stepDateStr>2014-03-24</stepDateStr><stepMessage>结束</stepMessage></return>

                      </ns2:getOrderStepListByOrderIdResponse></soap:Body></soap:Envelope>

                             每一个return元素代表的是一条订单跟踪信息,“stepDateStr”代表的是跟踪时间,“stepMessage”代表的是跟踪信息。按照时间顺序进行排列。

 

方式三:使用URLCONNCTION的方式进行调用

适用场景

android,不需要导入大量的java代码

使用步骤

                    参见示例

通过urlconnection的方式调用Webservice服务

public class App {

 

public static void main(String[] args) throws Exception {

//声明webservice服务的地址

URL wsUrl = newURL("http://192.168.6.89:8090/custom/cxf/order/");

 

//打开连接

HttpURLConnection conn = (HttpURLConnection)wsUrl.openConnection();

 

//设置

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type","text/xml;charset=UTF-8");

 

//构造请求体

String soap ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\""

+"xmlns:q0=\"http://order.webserver.kewei/\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "

+"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"

+'<soapenv:Body><q0:getOrderStepListByOrderId><arg0>123</arg0></q0:getOrderStepListByOrderId>

+"</soapenv:Body></soapenv:Envelope>";

 

OutputStream out = conn.getOutputStream();

out.write(soap.getBytes());

 

int code = conn.getResponseCode();

if(code == 200){

InputStream is = conn.getInputStream();

byte[] b = new byte[1024];

int len = 0;

StringBuffer sb = new StringBuffer();

while((len = is.read(b)) != -1){

String s = new String(b,0,len,"UTF-8");

sb.append(s);

}

System.out.println(sb.toString());

is.close();

}

out.close();

conn.disconnect();

}

}




              

            

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录....................................................................................................................................................2 说明....................................................................................................................................................3 一、使用到的开发工具:.........................................................................................................3 二、服务端开发步骤.................................................................................................................3 1、使用xfire 创建Web 服务。..............................................................................................3 2、创建服务接口和实现.........................................................................................................7 3、xfire 服务配置....................................................................................................................9 4、服务发布...........................................................................................................................10 5、为服务配置安全认证.......................................................................................................11 三、客户端开发.......................................................................................................................12 1、新建Web 工程................................................................................................................. 12 2、导入xfire 包.....................................................................................................................13 3、创建类定义.......................................................................................................................15 四、扩展1-使用xfire 插件创建客户端代码........................................................................ 18 1、插件安装.............................................................................................................................18 2、使用已安装的插件进行客户端代码生成........................................................................ 20 4、编写调用代码,调用服务.............................................................................................. 24 五、扩展2-使用axis(这时是1-4 版本)创建客户端代码.................................................... 28 1、使用WSDL2Java 工具根据WSDL 生成客户端代码.................................................. 28 2、新建工程项目...................................................................................................................30 3、编写调用代码...................................................................................................................31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值