- 浏览: 53551 次
文章分类
最新评论
-
蒙奇君杰:
煎蛋就是幸福 写道我想说,lz你英语水平真不咋的,错误信息提示 ...
tomcat在Debug模式下无法启动解决办法 -
煎蛋就是幸福:
我想说,lz你英语水平真不咋的,错误信息提示的是在45秒内启动 ...
tomcat在Debug模式下无法启动解决办法
WebService 搭建及调用
webservice的应用已经越来越广泛了,CXF开发webservice也是比较方便和简单的,它和spring的集成可以说是非常地好。举一个CXF开发webservice的例子吧。
一、环境搭建
首先,下载CXF,官网(http://cxf.apache.org/),具体位置如下图:
解压后,得到以下目录,如图所示:
将上图中解压的..\apache-cxf-2.7.6\lib目录中的所有文件复制到新建工程lib目录下特别是endorsed文件夹也要原样复制。
二、WebService的服务搭建
1.创建工程
在eclipse/myEclipse中建立web工程webService_cxf,创建方式点击菜单项“File”—“New”—“Web Service Project”,如下图:
弹出窗体,工程名名为webService_cxf,如下图:
2.新建接口文件TypeService.java
package com.flageader.service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
//必须有@WebService
@WebService
public interface TypeService {
//基本类型
public int testInt(short short1, short short2);
//数组
public String testArray(int[] int1,char[] char1, byte[] byte1) ;
//集合 list
public List testList(List list) ;
//集合 list<String>
public List<String> testListString(String[] str1) ;
//集合 map
public Map testMap(int id, String name, int age, char sex, String grade,String remark) ;
//日期
public Date testDate(Date date) ;
}
3.新建实现类TypeServiceImpl.java
package com.flageader.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TypeServiceImpl implements TypeService {
/**
* 常见类型 int
*/
public int testInt(short short1, short short2) {
int int1 = short1 + short2;
return int1;
}
/**
* 数组
*/
public String testArray(int[] int1, char[] char1, byte[] byte1) {
String str = "";
str+="<int[]={";
for (int i = 0; i < int1.length; i++) {
str += int1[i] + ",";
}
str+="}>\n<char[]={";
for (int i = 0; i < char1.length; i++) {
str += char1[i] + ",";
}
str+="}>\n<byte[]={";
for (int i = 0; i < byte1.length; i++) {
str += byte1[i] + ",";
}
str+="}";
return str;
}
/**
* 集合 list
*/
public List testList(List list) {
return list;
}
/**
* 集合 list<String>
*/
public List<String> testListString(String[] str1) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < str1.length; i++) {
list.add(str1[i]);
}
return list;
}
/**
* 集合 map
*/
public Map testMap(int id, String name, int age, char sex, String grade,
String remark) {
Map map = new HashMap();
map.put("id", id);
map.put("name", name);
map.put("age", age);
map.put("sex", sex);
map.put("grade", grade);
map.put("remark", remark);
return map;
}
/**
* 时间
*/
public Date testDate(Date date) {
return date;
}
}
4.新建applicationContext.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: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">
<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" />
<!-- implementor:为实现类的完整路径名 -->
<jaxws:endpoint id="typeService" implementor="com.flageader.service.TypeServiceImpl" address="/typeService" />
</beans>
5.修改配置文件web.xml
找到工程中web.xml文件,打开,将以下内容替换web.xml中内容,并保存
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-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-pattern>/webServices/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这样webService编程完成了,为了能知道创建的方法是否存在着问题,我们需要进行测试。
6.新建测试项
点击工程“webService_cxf”—“New”—“Source Folder”,如下图:
将其命名为test,如下图:
在点击实现类“TypeServiceImpl”—“New”—“Other...”,如下图:
弹出窗体,在窗体中的文本框中输入“test”,在点击“Junit Test Case”,如下图:
弹出窗体“Junit Test Case”,点击“Browse...”,弹出窗体“Source Folder Selection”,选择“webService_cxf”—“Test”,如下图:
点击下一步,如下图:
选择方法,如下图:
这样创建了测试类TypeServiceImplTest.java。
7.添加测试类TypeServiceImplTest.java
package com.flageader.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
public class TypeServiceImplTest extends TestCase {
public void testTestInt() {
TypeService test=new TypeServiceImpl();
short short1=12;
short short2=24;
System.out.println("----int------\n"+test.testInt(short1, short2));
}
public void testTestArray() {
TypeService test=new TypeServiceImpl();
int[] int1={1,3,5,7};
char[] char1={'男','女','是','否'};
byte[] byte1={0,1};
System.out.println("----数组------\n"+test.testArray(int1,char1, byte1));
}
public void testTestList() {
TypeService test=new TypeServiceImpl();
List list=new ArrayList();
list.add("ddd");
list.add(3.4);
List list1=test.testList(list);
System.out.println("----list------");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public void testTestListString() {
TypeService test=new TypeServiceImpl();
String[] strs={"张三","李四","王五","赵六"};
List<String> list=test.testListString(strs);
System.out.println("----list<String>------\n");
for (int i = 0; i < list.size(); i++) { System.out.println("----list<String>------\n"+list.get(i));
}
}
public void testTestMap() {
TypeService test=new TypeServiceImpl();
Map map=test.testMap(1, "李丽", 12, '男', "六年级", "无");
System.out.println("----map------\n"+map);
}
public void testTestDate() {
try {
TypeService test=new TypeServiceImpl();
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
Date date =test.testDate(sim.parse("2004-09-02"));
System.out.println("----date------\n"+date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
8.测试结果
得到测试结果如下图打印输出所示:
9.工程发布
myEclispe/Eclispe已经配置好Tomcat(如果没有请配置好),点击“Tomcat 6.x”—“Add Deployment...”,如下图:
弹出窗体,选择工程“webService_cxf”,如下图:
这样就发布了“webService_cxf”工程。
启动tomcat
访问路径:http://localhost/webService_cxf/webServices/typeService?wsdl
a)怎样知道访问路径
localhost— ip地址
在此tomcat端口号为80,可省略,其他端口号是不能省略的,如h
ttp://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服务。
1.新建规则包Client
点击工程“test1”—“新建规则包”,并命名为Client,如下图:
点击规则包“Client”下的“对象库”—“添加web服务向导”,如下图:
弹出窗体,在wsdl地址对应的文本框中输入http://localhost/webService_cxf/webServices/typeService?wsdl,点击查找,勾选方法, 如下图:
显示如下:
2.调用日期型接口
新建规则包“date”,添加web服务,选择如下服务,勾选参数,如下图:
勾选方法
对象库中参数定义,如下图:
规则配置如下,其中
测试结果
其他接口的调用方法也都类似,调用的时候一定要注意参数类型。
发表评论
-
规则引擎Visual Rules Solution开发基础教程【连载24】-- 使用VisualRules规则引擎实现业务逻辑
2016-04-29 09:33 870使用VisualRules规则引擎实现业务逻辑 ... -
规则引擎Visual Rules Solution开发基础教程【连载23】--规则协同管理之系统管理
2016-04-22 15:44 964规则协同管理之系统管 ... -
规则引擎Visual Rules Solution开发基础教程【连载22】--规则协同管理之信息维护
2016-01-25 09:12 972规则协同管理之信息维护 信息维护只有两个部分 ... -
规则引擎Visual Rules Solution开发基础教程【连载21】--规则协同管理之权限管理
2016-01-18 09:47 1286规则协同管理之权限管理 权限管理由三部分组成 ... -
规则引擎Visual Rules Solution开发基础教程【连载20】--规则协同管理之规则管理
2016-01-11 09:48 1435规则协同管理之规则管理 一、规则上传 我们 ... -
规则引擎Visual Rules Solution开发基础教程【连载19】--规则协同管理介绍
2016-01-07 09:20 631规则协同管理介绍 一、简介 在团队开发项目 ... -
规则引擎Visual Rules Solution开发基础教程【连载18】-- Linux环境 安装配置说明
2015-12-21 10:15 1227Linux环境 安装配置说明 一、安装配置测试环境及版本 ... -
规则引擎Visual Rules Solution开发基础教程【连载17】--VisualRules接口调用(六)
2015-12-16 11:28 989VisualRules接口调用(六) ——Java类中含类 ... -
规则引擎Visual Rules Solution开发基础教程【连载16】--VisualRules接口调用(五)
2015-12-07 09:09 1350VisualRules接口调用(五) Java类中的List ... -
规则引擎Visual Rules Solution开发基础教程【连载15】--VisualRules接口调用(四)
2015-11-30 09:11 1024VisualRules接口调用(四) 一、Java类调用服务 ... -
规则引擎Visual Rules Solution开发基础教程【连载14】--VisualRules接口调用(三)
2015-11-23 09:23 907VisualRules接口调用(三 ... -
规则引擎Visual Rules Solution开发基础教程【连载13】--VisualRules接口调用(二)
2015-11-16 10:31 894VisualRules接口调用(二) 接上一篇,下面介绍一下 ... -
规则引擎Visual Rules Solution开发基础教程【连载12】--VisualRules接口调用(一)
2015-11-09 10:25 1699VisualRules接口调用(一 ... -
规则引擎Visual Rules Solution开发基础教程【连载10】--VisualRules的组成及执行原理
2015-10-26 17:41 1141VisualRules的组成及执行原理 在 ... -
规则引擎Visual Rules Solution开发基础教程【连载9】--VisualRules实例二(下)
2015-10-19 10:46 842VisualRules实例二(下) 书接上 ... -
规则引擎Visual Rules Solution开发基础教程【连载8】--VisualRules实例二(中)
2015-10-12 10:11 996VisualRules实例二(中) 上一篇 ... -
规则引擎Visual Rules Solution开发基础教程【连载7】--VisualRules实例二(上)
2015-10-08 11:45 933VisualRules实例二(上) ... -
规则引擎Visual Rules Solution开发基础教程【连载6】--VisualRules实例一
2015-09-28 11:22 616VisualRules实例一 以下通过一个 ... -
规则引擎Visual Rules Solution开发基础教程【连载5】--VisualRules深入了解
2015-09-21 10:02 1662VisualRules深入了解 通过前两篇的 ... -
规则引擎Visual Rules Solution开发基础教程【连载4】--VRS相关调用篇
2015-09-14 10:10 943VRS相关调用 一、Java类 ...
相关推荐
公司代码实现,简单的webservice服务 发布,实现客户端的调用。
Laravel开发-laravel-webservice 基于httpful的Laravel Web服务提供商
maximo6-JAVA调用WEBSERVICE
XFire-WebService 开发基础教程 有图有真相 XFire-WebService 开发基础教程XFire-WebService 开发基础教程
MyEclipse基于JAX-WS开发Webservice+WebserviceClient客户端调用.zip
springboot+webservice搭建webservice服务端及使用java客户端两种方式进行调用webservice接口
Spring+Spring-ws+maven搭建WebService服务端demo用例
ETL工具(kettle)使用系列(四)-kettle调用webservice数据插入数据库-真实案例脱密处理
Laravel开发-systempay-webservice 该库提供了一个简单快速的SystemPayWebService包。Web服务用于将一个或多个支付功能包含到集成的CMS中。
C#调用-PB做的WEBSERVICE
maven-spring-mybatis-webservice整合.
CXF入门教程(4) -- webService异步调用模式文章配套代码,文中涉及的异步调用客户端的代码放在com.neareast.test.cxf.asyClient包下。原文地址:http://blog.csdn.net/neareast/article/details/7726503
springboot+webservice搭建webservice服务端及使用java客户端两种方式进行调用
Android高级应用源码-Android调用Webservice.zip
dubbo-rpc-webservice-2.8.4 dubbo-rpc-webservice-2.8.4
使用MuleEsb同时掉多个webservice,并返回到调用方。开发环境为AnyPoint3.7,将例子中的webservice接口修改成自己的即可运行。
jax-ws-webservice开发 通过myeclipse进行 jax-ws的开发及其客户的编写
.net调用webservice接口例子.net调用webservice接口例子.net调用webservice接口例子.net调用webservice接口例子
Java-Spring-WebService最基础的配置示例.txt