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();
}
}