Web Service -- 使用CXF开发入门
2015-07-18 15:43
405 查看
【使用CXF开发】
一、使用CXF开发Web Service服务器端:
/******************************
每个Web Service组件需要两个部分,接口和实现类
*******************************/
1.开发一个Web Service业务接口(该接口要用@WebService修饰)
2.开发一个Web Service实现类(实现类也需要用@WebService修饰)
3.使用EndPoint类的静态方法来发布Web Service
服务端依赖的jar包:
![](http://img.blog.csdn.net/20150718154832682?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
二、使用CXF开发Web Service客户端:
配置CXF的环境变量
cmd中进入到client的src目录下,执行命令: wsdl2java命令 + 服务端发布的地址
![](http://img.blog.csdn.net/20150718155356206?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
一、使用CXF开发Web Service服务器端:
/******************************
每个Web Service组件需要两个部分,接口和实现类
*******************************/
1.开发一个Web Service业务接口(该接口要用@WebService修饰)
2.开发一个Web Service实现类(实现类也需要用@WebService修饰)
3.使用EndPoint类的静态方法来发布Web Service
package com.jjyy.server.service; import java.util.List; import javax.jws.WebService; import com.jjyy.server.domain.Cat; import com.jjyy.server.domain.User; @WebService public interface HelloWorld { public void sayHello(String name); List<Cat> getCatsByName(User user); }
package com.jjyy.server.service.impl; import java.util.List; import javax.jws.WebService; import com.jjyy.server.domain.Cat; import com.jjyy.server.domain.User; import com.jjyy.server.service.HelloWorld; import com.jjyy.server.service.UserService; @WebService(endpointInterface="com.jjyy.server.service.HelloWorld",name="helloWorld") public class HelloWorldWS implements HelloWorld { @Override public void sayHello(String name) { System.out.println("当前时间是:"+System.currentTimeMillis()+"==="+name); } @Override public List<Cat> getCatsByName(User user) { //在实际的项目中,Web Service 组件自己并不会去实现业务功能, //它只是调用业务逻辑组件的方法来暴露Web Service UserService us = new UserServiceImpl(); return us.getCatsByUser(user); } }
package com.jjyy.server.service; import java.util.List; import com.jjyy.server.domain.Cat; import com.jjyy.server.domain.User; public interface UserService { List<Cat> getCatsByUser(User user); }
package com.jjyy.server.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.jjyy.server.domain.Cat; import com.jjyy.server.domain.User; import com.jjyy.server.service.UserService; public class UserServiceImpl implements UserService { static Map<User, List<Cat>> catDb = new HashMap<User, List<Cat>>(); static{ List<Cat> catList01 = new ArrayList<Cat>(); catList01.add(new Cat(1, "jy01", "yellow")); catList01.add(new Cat(2, "jy02", "pink")); List<Cat> catList02 = new ArrayList<Cat>(); catList02.add(new Cat(3, "jy03", "yellow")); catList02.add(new Cat(4, "jy04", "pink")); catDb.put(new User(1, "jy1", "123456", "bj"), catList01); catDb.put(new User(2, "jy2", "123456", "bj"), catList02); } @Override public List<Cat> getCatsByUser(User user) { return catDb.get(user); } }
package com.jjyy.server.main; import javax.xml.ws.Endpoint; import com.jjyy.server.service.HelloWorld; import com.jjyy.server.service.impl.HelloWorldWS; public class ServerMain { public static void main(String[] args) { HelloWorld hw = new HelloWorldWS(); //调用endPont的publish发布webService Endpoint.publish("http://192.168.0.138/creazy", hw); System.out.println("web Service start .... ed"); } }
服务端依赖的jar包:
二、使用CXF开发Web Service客户端:
配置CXF的环境变量
cmd中进入到client的src目录下,执行命令: wsdl2java命令 + 服务端发布的地址
package com.jjyy.client.main; import java.util.List; import com.jjyy.server.service.Cat; import com.jjyy.server.service.HelloWorld; import com.jjyy.server.service.User; import com.jjyy.server.service.impl.HelloWorldWSService; public class ClientMain { public static void main(String[] args) { HelloWorldWSService factory = new HelloWorldWSService(); //返回web Service的远程代理 HelloWorld hw = factory.getHelloWorldPort(); hw.sayHello("jiangyhu"); User user = new User(); user.setName("jy1"); user.setPass("123456"); List<Cat> list = hw.getCatsByName(user); for (Cat cat : list) { System.out.println(cat.getName()+"=="+cat.getColor()); } } }
相关文章推荐
- Java中的五种单例模式实现方法
- 字符串面试题:将字符串转换为整数
- 黑马程序员——Java基础---异常
- C++ string.replace的使用
- C++ string.replace的使用
- 什么叫大数据?什么叫O2O?什么叫蓝海?什么叫红海?什么叫互联网思维?什么叫众筹?
- 九度oj 1054
- 如何设置两个元件靠近后不变绿不报警
- Web Service介绍
- 数据结构--堆排序
- 未来网络的主宰——云操作系统
- 算法导论 第二十一章:不相交集合的数据结构
- Android入门第八篇之GridView(九宫图)
- 如何从string 转换为const char *和char * (附带string中c_str()、data()、copy(p,n)函数的用法)
- X240-win8.1无法连接无线网络的解决方案
- 字符串面试题:将句子的单词序倒置
- ECshop彻底去版权(同时适用于2.7.3)
- ECshop彻底去版权(同时适用于2.7.3)
- java 内存数据的条件筛选和分页处理
- codevs1033