场景

某SpringMVC项目原本为一个HTTP的WEB服务项目,之后想在该项目中添加WebService支持,使该项目同时提供HTTP服务和WebService服务。其中WebService服务通过 /ws/** 地址拦截。

配置

通过配置让SpringMVC支持WebService。

依赖

首先通过Maven引入必要依赖包。

  • org.apache.cxf
  • org.apache.neethi
  • com.ibm.wsdl4j
  • org.apache.XmlSchema

Web.xml

通过配置Web.xml使Spring框架具备WebService特性,这里通过添加Servlet(这里使用CXFServlet)实现。假设SpringMVC本身的DispatcherServlet已经启用,则在第2启动顺序添加CXFServlet。并添加servlet-mapping匹配请求。

配置如下

<!-- 在上下文中添加配置文件 -->
<context-param>
<param-name>patchConfigLocation</param-name>
<param-value>
/WEB-INF/applicationServlet.xml
/WEB-INF/webservice.xml
<param-value>
</context-param>
<!-- 添加servlet -->
<servlet>
<servlet-name>ws</servlet-name>
<servlet-class>org.apache.cxf.trasport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/ws/**</url-pattern>
</servlet-mapping>

webservice.xml

将webservice的接口配置单独分离出来。配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- cxf必要配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <!-- 接口的实现类声明 -->
<jaxws:endpoint id="ticketDecodeAuthService"
implementorClass="com.xxx.apps.web.ws.server.decode.XXXServiceImpl"
address="/ticketDecodeAuth" /> </beans>

接口编写

对应上文声明的接口文档在写在相应的位置上(如本文例子则写在com.xxx.apps.web.ws.server.decode包中)

代码如下:

@WebService
@SOAPBinding(style = Style.RPC)
public interface XXXService { public WSReturn getAuth(String userName, String password) throws Exception; }

接口实现类:

@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class XXXServiceImpl implements XXXService { private static final Logger LOGGER = Logger.getLogger(XXXServiceImpl.class); @Override
public WSReturn getAuth(String userName, String password) throws Exception {
// WSReturn 是自定义的通用接口返回包装,可以用别的
WSReturn res = new WSReturn();
// TODO : your code here
return res;
} }

发布接口效果

启动SpringMVC项目,根据配置文件定义,接口地址类似:http://ip:port/项目名/ws/**

若本例配置则有如下接口可以查看:

查看所有接口列表

http://ip:port/项目名/ws

某具体端口(XXXService)为例

这也是客户端调用时候的地址

http://ip:port/项目名/ws/XXXService?wsdl

这里可以看到端口的规范定义

客户端编写

客户端代码

通过CXF的动态代理方式编写,以反射方式将class直接引入可以实现统一调用方法。这样该Client即可调用任意接口。

代码如下:

/**
* webservice服务客户端
* @author WSY
*
*/
public class WSClient { private static Logger logger = LoggerFactory.getLogger(WSClient.class); /**
* 调用代理
* @param cls 服务接口代理
* @param method 方法名
* @param wsdl wsdl地址
* @param params 参数Object[]
* @return
* @throws Exception
*/
@SuppressWarnings("rawtypes")
public static WSReturn invoke(Class cls,String method,String wsdl, Object[] params) throws Exception{
synchronized(WSClient.class){
logger.info("[WSClient invoking] - class:"+cls.getName()+"; method:"+method+"; wsdl:"+
wsdl+"; params:"+getParams(params)); JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(cls);
factory.setAddress(wsdl);
Object cInstance = factory.create();
Method invokeMethod = null;
for(Method m : cls.getDeclaredMethods()){
if(m.getName().equalsIgnoreCase(method)){
invokeMethod = m;
break;
}
}
if(invokeMethod == null)
throw new Exception("ERROR:method not found"); WSReturn res = (WSReturn) invokeMethod.invoke(cInstance, params);
return res;
} private static String getParams(Object[] params){
StringBuilder sb = new StringBuilder("{");
for(Object b : params){
sb.append(b).append(",");
}
if(sb.length()==1)
return "{}";
else
return sb.substring(0,sb.length()-1)+"}";
}
}
}

打包

写个Ant脚本将一些必要的Java类和定义的Interface(不要打实现类)打成包。本文中将Client代码也写在了Service端了,所以将WSClient也一并打包进去。这样在编写对应的客户端时候,仅需专注于功能实现即可。

<?xml version="1.0"?>
<project name="tws-interfaces" default="jar" basedir="."> <!-- Give user a chance to override without editing this file or typing -D -->
<property name="coredir" location="." />
<property name="classdir" location="${basedir}/target/classes" /> <target name="jar" description="Build the jars for core">
<delete file="${coredir}/webservice-interfaces-1.0.jar" />
<jar destfile="${coredir}/webservice-interfaces-1.0.jar">
<fileset dir="${classdir}">
<include name="**/com/xxx/apps/web/ws/server/**/*Service.class" />
<include name="**/com/xxx/apps/web/ws/server/tokenservice/**/*.class" />
<include name="**/com/xxx/apps/web/ws/server/WSReturn.class"/>
<include name="**/com/xxx/apps/comm/ResultState.class"/>
<include name="**/com/xxx/apps/web/ws/server/wsclient/WSClient.class"/>
<include name="**/com/xxx/apps/comm/RespResult.class"/>
<exclude name="**/com/xxx/apps/web/ws/server/**/*Impl.class" />
</fileset>
</jar>
<copy todir="../xxxclient/lib" file="./webservice-interfaces-1.0.jar"></copy>
</target> </project>

客户端项目实现

依赖

首先通过Maven引入必要依赖包。

  • org.apache.cxf.cxf-rt-frontend-jaxws
  • org.apache.cxf.cxf-rt-databinding-aegis
  • org.apache.cxf.cxf-rt-transports-http
  • org.apache.cxf.cxf-rt-transports-http-jetty
  • commons-codec.commons-codec

    最重要的:引入server端打包好的jar包,里边有WSClient和必要的接口

        <dependency>
    <groupId>com.xxx</groupId>
    <artifactId>xxxserver</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/webservice-interfaces-1.0.jar</systemPath>
    </dependency>

WSClient调用

通过直接调用jar包中的WSClient即可调用远程WebService接口。

调用示例代码如下:

/** 这里将调用注释复制过来
* 调用代理
* @param cls 服务接口代理
* @param method 方法名
* @param wsdl wsdl地址
* @param params 参数Object[]
* @return
* @throws Exception
*/
WSReturn res= WSClient.invoke(XXXService.class
, "getAuth"
,endpoints.get(XXXService.class.getName())
, new Object[]{"admin","admin"});
if(token.getStatusId() == ResultState.SUCESS){
tokenValue = (String) token.getMap().get("token");
} else {
logger.error("获取token失败:"+token.getMsg());
}

一些坑

一定要引入cxf的必要配置

虽然在项目中看不到,但是这些xml文件在cxf的jar包中。

    <!-- cxf必要配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

Interface的类路径一定要统一

如服务端的 XXXService.javacom.xxx.web.ws.server 中,则在客户端的XXXService.java类也应该在相同的路径即: com.xxx.web.ws.server 。 所以为方便起见,用Ant直接打包比较方便,不容易错。

客户端并发问题

本例中调用WSClient,通过反射机制调用,共用一个Factory,因此在并发时候容易出现问题,需要在WSClient中加锁

原文地址:https://blog.csdn.net/tzdwsy/article/details/51938786

如何在SpringMVC项目中部署WebService服务并打包生成客户端的更多相关文章

  1. JAVA项目中公布WebService服务——简单实例

    1.在Java项目中公布一个WebService服务: 怎样公布? --JDK1.6中JAX-WS规范定义了怎样公布一个WebService服务. (1)用jdk1.6.0_21以后的版本号公布. ( ...

  2. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  3. 如何在maven项目中使用spring

    今天开始在maven项目下加入spring. 边学习边截图. 在这个过程中我新建了一个hellospring的项目.于是乎从这个项目出发开始研究如何在maven项目中使用spring.鉴于网上的学习资 ...

  4. Docker & k8s 系列三:在k8s中部署单个服务实例

    本章将会讲解: pod的概念,以及如何向k8s中部署一个单体应用实例. 在上面的篇幅中,我们了解了docker,并制作.运行了docker镜像,然后将镜像发布至中央仓库了.然后又搭建了本机的k8s环境 ...

  5. [Laravel-Swagger]如何在 Laravel 项目中使用 Swagger

    如何在 Laravel 项目中使用 Swagger http://swagger.io/getting-started/ 安装依赖 swagger-php composer require zirco ...

  6. 如何在cocos2d项目中enable ARC

    如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...

  7. 如何在NodeJS项目中优雅的使用ES6

    如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...

  8. 如何在VUE项目中添加ESLint

    如何在VUE项目中添加ESLint 1. 首先在项目的根目录下 新建 .eslintrc.js文件,其配置规则可以如下:(自己小整理了一份),所有的代码如下: // https://eslint.or ...

  9. VS2015 项目中 添加windows服务

    1. 在项目中添加winows服务 今天刚刚为自己的项目添加了windows服务,以服务的形式运行后台系统,为前端提供接口服务,下面说一下具体怎么为vs项目添加windows服务 2. 添加Windo ...

随机推荐

  1. agc015D A or...or B Problem

    题意:求用若干个(至少一个)[A,B]中的数进行or操作能得到多少本质不同的数 $1 \leq A \leq B < 2^{60}$ 一直在想数位dp,看了题解之后感觉自己就是个sb 我们先把$ ...

  2. pycharm 测试执行成功,但却无法成功生成测试报告(使用HTMLTestRunner)的解决办法

    pycharm 测试执行成功,在对应的测试路径下确未生成测试报告.反复确认代码也是没有问题的,在网上查找了原因:简单的unittest运行是不执行main方法的.是允许方式问题. 于是在mian方法里 ...

  3. locationManager 回调方法不调用问题?

    当locationManager都设置好了后开始定位服务后回调方法didUpdateToLocation不调用 [_locationManager setDelegate:self]; [_locat ...

  4. SQL —— 存储过程

    一.什么是存储过程 预先存储好的SQL程序. 保存在SQL Server中(跟视图的存储方式一样) 通过名称和参数执行. 二.存储过程的优点 执行速度更快 允许模块化程序设计 提高系统安全性 减少网络 ...

  5. win10下安装mongodb(解压版)

    首先到官网下载安装包.(https://www.mongodb.com/download-center#community) 1.创建mongodb目录 2.配置文件mongodb.config 3. ...

  6. JavaScript 生成32位UUID

    function uuid(){ var len=32; //32长度 var radix=16; //16进制 var chars='0123456789ABCDEFGHIJKLMNOPQRSTUV ...

  7. POJ1190 洛谷P1731 NOI1999 生日蛋糕

    生日蛋糕(蛋糕是谁?) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20272   Accepted: 7219 Desc ...

  8. C++ 浮点数 为 0 的判断

  9. 大数据技术之Hadoop入门

      第1章 大数据概论 1.1 大数据概念 大数据概念如图2-1 所示. 图2-1 大数据概念 1.2 大数据特点(4V) 大数据特点如图2-2,2-3,2-4,2-5所示 图2-2 大数据特点之大量 ...

  10. 学习python所需要了解的一些基础计算机知识汇总

    1)编程语言 语言是一个物体与另一个物体交流的介质,而编程语言就是程序员与计算机沟通的介质,人使用编程语言的目的就是控制计算机为人服务. 例如,用户使用用python语言编写的应用程序通过操作系统向C ...