使用CXF实现Webservice的服务接口提供以及相关的客户端实现
至apache官网下载相关的jar包。
一。 编写提供服务的接口以及相关实现类
package com.demo;import java.util.List;import javax.jws.WebParam;
import javax.jws.WebService;import test.User;@WebService
public interface HelloWorld {String sayHi(@WebParam(name = "text")String text);String sayHiToUser(User user);String[] SayHiToUserList(List<User> userList);
}
package com.demo;import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;import javax.jws.WebService;import test.User;@WebService(endpointInterface = "com.demo.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {Map<Integer, User> users = new LinkedHashMap<Integer, User>();public String sayHi(String text) {return "Hello " + text;}public String sayHiToUser(User user) {users.put(users.size() + 1, user);return "Hello " + user.getName();}public String[] SayHiToUserList(List<User> userList) {String[] result = new String[userList.size()];int i = 0;for (User u : userList) {result[i] = "Hello " + u.getName();i++;}return result;}}
二。 无WEB容器,无Spring相关代码,仅通过JAVA项目代码模拟实现服务端接口提供以及客户端调用例子。
1. JAVA模拟服务端提供服务代码:
package test;import javax.xml.ws.Endpoint;import com.demo.HelloWorldImpl;public class WebServiceAppServer {public static void main(String[] args) {System.out.println("web service start");HelloWorldImpl implementor = new HelloWorldImpl();String address = "http://localhost:8080/helloWorld";Endpoint.publish(address, implementor);System.out.println("web service started");}
}
2. 运行如上代码,就可以模拟实现JAVA代码提供webservice服务,在浏览器中直接运行 http://localhost:8080/helloWorld?wsdl即可发现服务已提供。
3. JAVA客户端代码实现调用服务接口:
package test;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import com.demo.HelloWorld;public class HelloWorldNoSpringClient {public static void main(String[] args) {// TODO Auto-generated method stubJaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();svr.setServiceClass(HelloWorld.class);svr.setAddress("http://localhost:8080/helloWorld");HelloWorld hw = (HelloWorld) svr.create();User user = new User();user.setName("Tony");System.out.println(hw.sayHiToUser(user));}}
三。 通过WEB容器,以及Spring实现服务的提供。以及客户端的代码调用例子。
1. 在web.xml中配置相关信息,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFServlet</servlet-name><display-name>CXFServlet</display-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>/webservice/*</url-pattern></servlet-mapping></web-app>
2. 在src目录下,直接新增一个配置文件,如下:
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl" address="/helloWorld" /><bean id="client" class="com.demo.HelloWorld" factory-bean="clientFactory" factory-method="create" /><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="com.demo.HelloWorld" /><property name="address" value="http://localhost:8080/CXF/webservice/helloWorld" /></bean>
</beans>
3. 通过获取applicationContext.xml文件中的配置文件来获取相关BEAN,并调用服务端服务。
package test;import java.util.ArrayList;
import java.util.List;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.demo.HelloWorld;public class HelloWorldWithSpringClient {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");HelloWorld client = (HelloWorld) context.getBean("client");User user1 = new User();user1.setName("Tony");User user2 = new User();user2.setName("freeman");List<User> userList = new ArrayList<User>();userList.add(user1);userList.add(user2);String[] res = client.SayHiToUserList(userList);System.out.println(res[0]);System.out.println(res[1]);}}
4. 也可以通过直接 组装获取参数来进行服务端接口调用。
// TODO Auto-generated method stubJaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();svr.setServiceClass(HelloWorld.class);svr.setAddress("http://localhost:8080/helloWorld");HelloWorld hw = (HelloWorld) svr.create();User user = new User();user.setName("Tony");System.out.println(hw.sayHiToUser(user));
使用CXF实现Webservice的服务接口提供以及相关的客户端实现相关推荐
- Java调用WebService(asmx)服务接口
导入httpclient jar <dependency><groupId>commons-httpclient</groupId><artifactId&g ...
- Web Service 一些对外公开的网络服务接口以及http://www.webxml.com.cn/zh_cn/index.aspx
Web Service 一些对外公开的网络服务接口 2011-10-29 14:12 商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endp ...
- 一些免费的WebService的服务网站(转发)
免费:网站首页 http://www.webxml.com.cn/zh_cn/web_services.aspx 例如: [新] 中文<->英文双向翻译WEB服务 获得标准数据 End ...
- 免费使用WebService的服务网站
一个提供开发者使用比较实用的综合性WebService的网站: http://www.webxml.com.cn/zh_cn/index.aspx 以下是一些对应的WebService: 注:有的We ...
- 第6章 服务模式 Service Interface(服务接口)
Service Interface(服务接口) 上下文 您正在设计企业应用程序,并且需要能够通过网络使用其部分功能.此功能需要能够被各类系统使用,因此互操作性是设计的重要方面.除互操作性之外,可能还需 ...
- DSF框架使用(DAO、序列化、注解、服务接口、服务代理)
1.首先说一下什么是DSF DSF(到家服务框架):提供服务接口供客户端调用,实现服务端服务共享.避免服务的不一致及二次开发:(个人理解:类似于对外提供的API接口-只是这里是提供的服务的接口) 2. ...
- 智能驾驶功能软件平台设计规范第五部分:定位功能服务接口
1 规范应用范围 本规范规定了智能驾驶功能软件平台的定位功能服务接口. 本规范适用于设计开发 GB/T<汽车驾驶自动化分级>[1]所定义的 2 级及以上的驾驶自动化系统即智能驾驶系统. 2 ...
- cxf开发webservice服务端怎么返回固定的报文格式_Spring boot webservice怎么玩? 第277篇...
相关历史文章(阅读本文之前,您可能需要先看下之前的系列?) WebService SOAP概述 - 第275篇 WSDL是什么"Lese" - 第276篇 一.前言 当官不为民做主 ...
- Springboot 基于CXF构建WebService服务
前言 最近因为系统需要接入了一个新的支付通道,一般来说都是使用RestApi 来接入,但是本次接入的支付通道为境外支付,使用的WebService,对于WS我们在实际业务中基本上不会用到,所以查阅了一 ...
最新文章
- 2020牛客多校第三场[C Operation Love+基础计算几何 判断多边形顺逆时针]
- 超图桌面版制作一幅简单专题图示例
- 135 页的《机器学习速查手册》,公式、图表都有,附下载!
- WPF系列学习之三(路由事件)
- Customizing Download - product hierarchy
- 富士康筹划在越南建造2.7亿美元新工厂,扩大生产线!
- python十大必备知识_python学习必备知识汇总
- String s = new String(“abc“)创建了几个对象
- 190702每日一句 孤独之前是迷茫,孤独之后是成长
- mysql slave_pending_jobs_size_max_MySQL MTS复制: hitting slave_pending_jobs_size_max
- C语言怎么实现语音功能
- Radare2 学习笔记:从入门到精通 1. Radare2 简介,及安装
- Using Sketch with Framer 使用Sketch与Framer Lynda课程中文字幕
- python修改zip文件内容_python操作zip文件
- 腾讯优测-优社区干货精选 | android开发在路上:少去踩坑,多走捷径(上)
- 基于OHCI的USB主机 —— UFI数据结构3
- java图文验证码登录验证
- 怎么将webp在线转改成jpg格式?
- 生命诚可贵!就让我们好好珍惜
- 台式计算机上的音箱应怎么接,台式电脑音箱和两个扬声器应该怎样连接