webService是用于解决分布式系统通信的一种解决方案。Apachecxf是当前主流的webService开发框架,由Apache提供。

  Apachecxf的webService开发,主要分为两种服务提供方式:Ws和Rs。JAX-WS传输数据使用的是xml格式,基于soap协议,而JAX-RS传输数据使用的是xml或者json,基于http协议。首先,来看一下JAX-WS的独立服务使用:

  1.建立一个maven的java项目

  

  2.在项目中导入cxf的jar包

    

     使用maven的坐标

<!-- 使用jax-ws进行开发 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- 内置jetty服务器 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency>

  3.编写服务器端程序

  编写实体类

package com.xx.cn.cxf_ws__helloworld.domain;public class Car {private Integer id;private String carName;private Double price;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCarName() {return carName;}public void setCarName(String carName) {this.carName = carName;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}@Overridepublic String toString() {return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";}}

  

package com.xx.cn.cxf_ws__helloworld.domain;import java.util.ArrayList;
import java.util.List;public class User {private Integer id;private String username;private String city;private List<Car> cars = new ArrayList<Car>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public List<Car> getCars() {return cars;}public void setCars(List<Car> cars) {this.cars = cars;}}

  编写服务

package com.xx.cn.cxf_ws__helloworld.service;import java.util.List;import javax.jws.WebMethod;
import javax.jws.WebService;import com.xx.cn.cxf_ws__helloworld.domain.Car;
import com.xx.cn.cxf_ws__helloworld.domain.User;//编写webService服务器端接口@WebService
public interface IUserService {@WebMethodpublic String sayHello(String name);@WebMethodpublic List<Car> findCarsByUser(User user);
}

  

package com.xx.cn.cxf_ws__helloworld.service;import java.util.ArrayList;
import java.util.List;import javax.jws.WebService;import com.xx.cn.cxf_ws__helloworld.domain.Car;
import com.xx.cn.cxf_ws__helloworld.domain.User;//编写webService接口实现类
@WebService(endpointInterface="com.xx.cn.cxf_ws__helloworld.service.IUserService",serviceName="userService")
public class IUserServiceImpl implements IUserService{public String sayHello(String name) {// 简单参数传递return "Hello" + name;}public List<Car> findCarsByUser(User user) {// 复杂参数传递List<Car> cars = new ArrayList<Car>();Car car = new Car();car.setId(1);car.setCarName("大众");car.setPrice(200000d);cars.add(car);user.setCars(cars);return cars;}}

  发布服务

package com.xx.cn.cxf_ws__helloworld.server;import javax.xml.ws.Endpoint;import com.xx.cn.cxf_ws__helloworld.service.IUserService;
import com.xx.cn.cxf_ws__helloworld.service.IUserServiceImpl;//发布jax_ws服务
public class CXFServer {public static void main(String[] args) {//服务实现对象IUserService userService = new IUserServiceImpl();//发布服务地址String address = "http://localhost:9909/userService";//发布服务Endpoint.publish(address, userService);System.out.println("服务已经启动...");}
}

  服务访问地址

http://localhost:9909/userService?wsdl

  编写客户端操作

package com.xx.cn.cxf_ws__helloworld.client;import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import com.xx.cn.cxf_ws__helloworld.service.IUserService;//WS客户端
public class WSClient {public static void main(String[] args) {//编写客户端调用发布的webService服务JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();jaxWsProxyFactoryBean.setAddress("http://localhost:9909/userService");jaxWsProxyFactoryBean.setServiceClass(IUserService.class);//添加日志监控jaxWsProxyFactoryBean.getInInterceptors().add(new LoggingInInterceptor());jaxWsProxyFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());//创建远程代理对象IUserService proxy = (IUserService) jaxWsProxyFactoryBean.create();//调用代理对象任何一个方法,都将通过网络调用web服务System.out.println(proxy.sayHello("琼华派"));}
}

  接下来,介绍一下WS整合spring进行开发

  1.建立mavenweb工程

    

  2.基于maven导入坐标

 <dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.1.7.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies><build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>tomcat-maven-plugin</artifactId><version>1.1</version><configuration><port>9800</port></configuration></plugin></plugins></build>

  3.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><!-- spring配置文件位置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- spring核心监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFService</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFService</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>

  4.导入先前写好的domain和service

  5.在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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><!-- address:客户端访问服务路径serviceClass:配置接口serviceBean:配置实现类 --><jaxws:server id="userService" address="/userService" serviceClass="com.xx.cn.cxf_ws__helloworld.service.IUserService"><jaxws:serviceBean><bean class="com.xx.cn.cxf_ws__helloworld.service.IUserServiceImpl" /></jaxws:serviceBean></jaxws:server></beans>

  访问地址

http://localhost:9800/cxf_ws__spring/services/userService?wsdl

  6.整合spring,编写客户端

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:client id="userServiceClient" serviceClass="com.xx.cn.cxf_ws__helloworld.service.IUserService" address="http://localhost:9800/cxf_ws__spring/services/userService" ><jaxws:inInterceptors><bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/></jaxws:inInterceptors><jaxws:outInterceptors><bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" /></jaxws:outInterceptors></jaxws:client>
</beans>

  

package cxf_ws__spring.test;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.xx.cn.cxf_ws__helloworld.domain.User;
import com.xx.cn.cxf_ws__helloworld.service.IUserService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class UserService_WS_Test {@Autowired@Qualifier("userServiceClient")private IUserService userService;@Testpublic void testCall() {System.out.println(userService.sayHello("琼华派"));User user = new User();user.setUsername("xiaoming");System.out.println(userService.findCarsByUser(user));}}

  

转载于:https://www.cnblogs.com/god-1949-keshi/p/8283948.html

webService的简单使用相关推荐

  1. vue 调用webservice_js跨域调用WebService的简单实例

    步骤1. 在web.config中的system.web节点里加入 步骤2.webservice代码 using System; using System.Collections.Generic; u ...

  2. vs.net 2005中引用webservice的简单方法

    vs.net 2005中引用webservice的简单方法 以往在vs.net 2003中,要add web reference的话,要输入要引用的webservice的wsdl的完全地址,然后点&q ...

  3. webservice实例java_Java WebService(实战) 简单实例

    一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService ...

  4. WebService的简单实现

    WebService的简单实现 一.socket主机创建和使用过程 1.socket()//创建套接字 2.Setsockopt()//将套接字属性设置为允许和特定地点绑定 3.Bind()//将套接 ...

  5. C# 创建、部署和调用WebService的简单示例

    C# 创建.部署和调用WebService的简单示例 webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互. 概念性的东西就不说太多,下面开始创建一个简单的webservice ...

  6. 用Java 开发 WebService Axis简单实例

    用 Java 开发 WebService Axis 简单实例 在开发之前, 先了解一下AXIS,axis目前出现的2版本以其灵活性,快速开发工具的集成高于xfire而使用者居其上.用Eclipse开发 ...

  7. java开发webservice简单实例_jsp实现的webservice的简单实例

    jsp webservice用到的比较少但是用到就是一个大东西了经常用过其它的api形式了而webservice用到不多,下面我们来看一篇关于jsp实现的webservice的简单实例吧,具体如下. ...

  8. 深圳Java培训:WebService的简单使用

    深圳Java培训:WebService的简单使用 我们在开发项目时,如果有需求需要从一个项目中去调用另一个项目中的资源,通常可以有四种方案: 1.RESTful 2.WebService 3.RPC ...

  9. C# 创建、部署和调用WebService的简单示例 webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互。 概念性的东西就不说太多,下面开始创建一个简单的webservi

    C# 创建.部署和调用WebService的简单示例 webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互. 概念性的东西就不说太多,下面开始创建一个简单的webservice ...

最新文章

  1. 什么是AWS Lambda?
  2. 机器学习实战第一步:特征选择与特征工程「附代码」
  3. python判断图片模糊
  4. 第四篇 HTML 表单深入了解、注释和a标签的运用
  5. 在Windows上部署AMP(Apache2.4+MySQL8.0+PHP7.2)
  6. 谈谈设计模式的几个原则
  7. 漫画算法:找出缺失的整数
  8. [css] 实现单行文本居中和多行文本左对齐并超出显示“...“
  9. mysql事务与jdbc事务_事务(mysql事务、jdbc事务)
  10. html5中提供的绘图元素,HTML5中Canvas元素的使用总结
  11. 面向对象之反射和其他内置方法
  12. java常量池方法区_Java方法区和运行时常量池溢出问题分析
  13. 仅一年,近半加密货币的“ICO”项目已死
  14. 【hightopo】【基础图标】 HT for Web简单图标的制作:进度图标
  15. Vue路由守卫(通俗易懂)
  16. 小程序数据分析工具:TalkingData
  17. 高等数学笔记-乐经良老师-第六章-微分方程
  18. python求线段长度_如何用python求线段长度
  19. QCustomPlot之盒须图(十六)
  20. dubbo系列三、 服务发现RegistryDirectory

热门文章

  1. C# 导出EXCEL文件
  2. 花生增产万书波谋定中国农民丰收节交易会 山东科技最高奖
  3. 精准扶贫探索新融合模式-农业大健康·李龙:谋定乡村振兴
  4. idea插件手动安装
  5. #035 大数阶乘 PTA题目6-10 阶乘计算升级版 (20 分)
  6. Mybatis学习第一天——Mybatis的安装配置以及基本CURD操作
  7. shell基本语法和执行
  8. href 和 src 的区别
  9. HTML5 本地存储
  10. MySQL 绿色版安装方法图文教程