WebService搭建及怎样在VRS中调用.docx
《WebService搭建及怎样在VRS中调用.docx》由会员分享,可在线阅读,更多相关《WebService搭建及怎样在VRS中调用.docx(31页珍藏版)》请在冰点文库上搜索。
![WebService搭建及怎样在VRS中调用.docx](https://file1.bingdoc.com/fileroot1/2023-6/2/f86efe38-ec37-488a-be62-f28435c3ebab/f86efe38-ec37-488a-be62-f28435c3ebab1.gif)
WebService搭建及怎样在VRS中调用
WebService
搭建及调用
杭州旗正信息技术有限公司
地址:
杭州市文三路199号7号楼512室
电话:
(860571)56771736
传真:
(860571)81110230
网址:
E-mail:
sale@
环境搭建3
WebService的服务搭建4
创建工程4
新建接口文件TypeService.java5
新建实现类TypeServiceImpl.java6
新建applicationContext.xml配置文件9
修改配置文件web.xml10
新建测试项12
添加测试类TypeServiceImplTest.java17
测试结果20
工程发布21
VRS调用WebService服务24
新建规则包Client25
调用日期型接口27
调用数组型接口30
调用map型接口33
调用list型接口36
总结说明38
环境搭建
首先,下载CXF,官网(http:
//cxf.apache.org/),具体位置如下图:
解压后,得到以下目录,如图所示:
将上图中解压的..\apache-cxf-2.7.6\lib目录中的所有文件复制到新建工程lib目录下特别是endorsed文件夹也要原样复制。
WebService的服务搭建
创建工程
在eclipse/myEclipse中建立web工程webService_cxf,创建方式点击菜单项“File”—“New”—“WebServiceProject”,如下图:
弹出窗体,工程名名为webService_cxf,如下图:
新建接口文件TypeService.java
packagecom.flageader.service;
importjava.util.Date;
importjava.util.List;
importjava.util.Map;
importjavax.jws.WebService;
//必须有@WebService
@WebService
publicinterfaceTypeService{
//基本类型
publicinttestInt(shortshort1,shortshort2);
//数组
publicStringtestArray(int[]int1,char[]char1,byte[]byte1);
//集合list
publicListtestList(Listlist);
//集合list
publicListtestListString(String[]str1);
//集合map
publicMaptestMap(intid,Stringname,intage,charsex,Stringgrade,Stringremark);
//日期
publicDatetestDate(Datedate);
}
新建实现类TypeServiceImpl.java
packagecom.flageader.service;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
publicclassTypeServiceImplimplementsTypeService{
/**
*常见类型int
*/
publicinttestInt(shortshort1,shortshort2){
intint1=short1+short2;
returnint1;
}
/**
*数组
*/
publicStringtestArray(int[]int1,char[]char1,byte[]byte1){
Stringstr="";
str+="for(inti=0;istr+=int1[i]+",";
}
str+="}>\nfor(inti=0;istr+=char1[i]+",";
}
str+="}>\nfor(inti=0;istr+=byte1[i]+",";
}
str+="}";
returnstr;
}
/**
*集合list
*/
publicListtestList(Listlist){
returnlist;
}
/**
*集合list
*/
publicListtestListString(String[]str1){
Listlist=newArrayList();
for(inti=0;ilist.add(str1[i]);
}
returnlist;
}
/**
*集合map
*/
publicMaptestMap(intid,Stringname,intage,charsex,Stringgrade,
Stringremark){
Mapmap=newHashMap();
map.put("id",id);
map.put("name",name);
map.put("age",age);
map.put("sex",sex);
map.put("grade",grade);
map.put("remark",remark);
returnmap;
}
/**
*时间
*/
publicDatetestDate(Datedate){
returndate;
}
}
新建applicationContext.xml配置文件
xmlversion="1.0"encoding="UTF-8"?
>
//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:
util="http:
//www.springframework.org/schema/util"
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
http:
//www.springframework.org/schema/util
http:
//www.springframework.org/schema/util/spring-util-2.0.xsd"
default-lazy-init="false">
META-INF/cxf/cxf.xml"/>
META-INF/cxf/cxf-extension-soap.xml"/>
META-INF/cxf/cxf-servlet.xml"/>
--implementor:
为实现类的完整路径名-->
endpointid="typeService"implementor="com.flageader.service.TypeServiceImpl"address="/typeService"/>
修改配置文件web.xml
找到工程中web.xml文件,打开,将以下内容替换web.xml中内容,并保存
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:
applicationContext.xml
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/webServices/*
index.jsp
index.jsp
这样webService编程完成了,为了能知道创建的方法是否存在着问题,我们需要进行测试。
新建测试项
点击工程“webService_cxf”—“New”—“SourceFolder”,如下图:
将其命名为test,如下图:
在点击实现类“TypeServiceImpl”—“New”—“Other...”,如下图:
弹出窗体,在窗体中的文本框中输入“test”,在点击“JunitTestCase”,如下图:
弹出窗体“JunitTestCase”,点击“Browse...”,弹出窗体“SourceFolderSelection”,选择“webService_cxf”—“Test”,如下图:
点击下一步,如下图:
选择方法,如下图:
这样创建了测试类TypeServiceImplTest.java。
添加测试类TypeServiceImplTest.java
packagecom.flageader.service;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importjava.util.Map;
importjunit.framework.TestCase;
publicclassTypeServiceImplTestextendsTestCase{
publicvoidtestTestInt(){
TypeServicetest=newTypeServiceImpl();
shortshort1=12;
shortshort2=24;
System.out.println("----int------\n"+test.testInt(short1,short2));
}
publicvoidtestTestArray(){
TypeServicetest=newTypeServiceImpl();
int[]int1={1,3,5,7};
char[]char1={'男','女','是','否'};
byte[]byte1={0,1};
System.out.println("----数组------\n"+test.testArray(int1,char1,byte1));
}
publicvoidtestTestList(){
TypeServicetest=newTypeServiceImpl();
Listlist=newArrayList();
list.add("ddd");
list.add(3.4);
Listlist1=test.testList(list);
System.out.println("----list------");
for(inti=0;iSystem.out.println(list.get(i));
}
}
publicvoidtestTestListString(){
TypeServicetest=newTypeServiceImpl();
String[]strs={"张三","李四","王五","赵六"};
Listlist=test.testListString(strs);
System.out.println("----list------\n");
for(inti=0;i------\n"+list.get(i));
}
}
publicvoidtestTestMap(){
TypeServicetest=newTypeServiceImpl();
Mapmap=test.testMap(1,"李丽",12,'男',"六年级","无");
System.out.println("----map------\n"+map);
}
publicvoidtestTestDate(){
try{
TypeServicetest=newTypeServiceImpl();
SimpleDateFormatsim=newSimpleDateFormat("yyyy-MM-dd");
Datedate=test.testDate(sim.parse("2004-09-02"));
System.out.println("----date------\n"+date);
}catch(ParseExceptione){
e.printStackTrace();
}
}
}
测试结果
得到测试结果如下图打印输出所示:
工程发布
myEclispe/Eclispe已经配置好Tomcat(如果没有请配置好),点击“Tomcat6.x”—“AddDeployment...”,如下图:
弹出窗体,选择工程“webService_cxf”,如下图:
这样就发布了“webService_cxf”工程。
启动tomcat
访问路径:
http:
//localhost/webService_cxf/webServices/typeService?
wsdl
a)怎样知道访问路径
●localhost—ip地址
●在此tomcat端口号为80,可省略,其他端口号是不能省略的,如http:
//localhost:
8080
●webService_cxf—工程名
●webServices是由web.xml配置文件决定,如下图:
●Typeservice是由applicationContext.xml配置文件决定,如下图:
●wsdl—web接口定义语言,在最后需要问号传参,即:
?
wsdl
b)访问
http:
//localhost/webService_cxf/webServices/typeService?
wsdl
完成。
VRS调用WebService服务
使用VisualRulesSolution规则配置器作为客户端调用WebService服务。
新建规则包Client
点击工程“test1”—“新建规则包”,并命名为Client,如下图:
点击规则包“Client”下的“对象库”—“添加web服务向导”,如下图:
弹出窗体,在wsdl地址对应的文本框中输入http:
//localhost/webService_cxf/webServices/typeService?
wsdl,点击查找,勾选方法,如下图:
显示如下:
调用日期型接口
新建规则包“date”,添加web服务,选择如下服务,勾选参数,如下图:
勾选方法
对象库中参数定义,如下图:
规则配置如下,其中
参数赋值:
取值需要:
结果赋值:
测试结果
调用数组型接口
新建规则包“array”,添加web服务,选择如下服务,勾选参数,其中“arg2”参数类型为base64Binary需要手动修改成base64Binary[],修改后点回车键,如下图:
勾选方法
对象库中添加如下参数,其中unsignedShort[]和base64Binary[]类型是选择不出来的,我们可以手动写出这些类型,修改后点回车键,参数如下:
规则如下:
测试结果,unsignedShort[]和base64Binary[]类型初始输入值不需要逗号“,”分隔
调用map型接口
新建规则包“map”,添加web服务,选择如下服务,勾选参数,如下图
勾选方法,其中返回结果集的方法的类型需要改成“map”,更改的方式是在属性中的“结果类型”和“返回值类型”对应的属性值修改为“map”,如下图:
设置如下参数,没有的类型,手动修改,在点击回车键,如arg3
规则配置如下图:
测试结果,其中arg3输入“男”得到的结果为“30007”,因此我们建议,尽量用String类型代替char类型,如下图:
调用list型接口
新建规则包“listString”,添加web服务,选择如下服务,勾选参数,如下图
勾选方法,myEclipse中编写的java方法,返回类型为List,在这将以string[]作为返回类型,如下图:
规则编写如下:
测试结果
其中Int和list的在此不做演示,需要修改类型的地方都是可以手动修改的,修改后点回车键。
总结说明
char类型不能很好的应用,解决方法是用String类型代替。
char类型在VisualRules中的web服务中unsignedShort,为了能匹配,在规则对象中定义参数的时候也许设置为unsignedShort。
byte类型在VisualRules中的web服务中base64Binary,为了能匹配,在规则对象中定义参数的时候也许设置为base64Binary。
list类型在VisualRules中的web服务中自动转化成E[],如list在web服务中String[]。
在VisualRules中的需要的类型如果选择不出来,可以手动修改,修改后点击回车键,web服务中返回值的类型需要在属性中修改。