近期项目间隙 自学了 webservice 一下 是我写的 一个demo
首先我们在web.xml 里配置如下
- <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>/services/*</url-pattern> //拦截qinqiu
- </servlet-mapping>
我用的 是maven项目 一下 pom.xml 引入一下依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.14</version>
<exclusions>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.14</version>
</dependency>
spring—cxf.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://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="lMSWebService"
implementor="wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.service.impl.UserWebServiceimpl"
address="/lMSWebService">
</jaxws:endpoint>
</beans>
接口列子
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.emty.User;
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.util.PageData;
@WebService(targetNamespace="http://impl.service.wenqiang.cn.wenqiang_mavenWeb.wenqiang_web/")
@SOAPBinding(style = Style.RPC)
public interface UserWebService {
String getUserByNameAndPwd(@WebParam(name = "pd")PageData pd);
}
接口实现类
public class UserWebServiceimpl implements UserWebService {
@Autowired
UserMapper usermapper;
public String getUserByNameAndPwd(@WebParam(name = "pd")PageData pd) {
Gson gson = new Gson();
String fenInformation = gson.toJson(usermapper.selectUserLogin(pd));
return fenInformation;
}
}
动态调用 工具类
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
/**
*
* 使用cxf 调用webservice 接口
*
* @author chenj
*
*/
public class CxfInvokeUtil {
static Logger logger = Logger.getLogger(CxfInvokeUtil.class);
/**
*
* 调用webservice 接口
*
* @param wsdlUrl wsdl 地址
*
* @param method 调用方法名
*
* @param params 接口传入参数
*
* @return
*
*/
public static synchronized Object[] invoke(String wsdlUrl,String method,Object... params){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient(wsdlUrl);
Object[] objects = null;
if(StringUtils.isEmpty(wsdlUrl)){
logger.error("cxf 调用webservice 参数缺失:wsdl url 未传入");
return objects;
}
if(StringUtils.isEmpty(method)){
logger.error("cxf 调用webservice 执行方法名缺失:method 未传入");
return objects;
}
try {
objects=client.invoke(method,params);
} catch (Exception e) {
e.printStackTrace();
logger.error("cxf 调用webservice 执行错误:",e);
}
return objects;
}
}
动态调用测试类
import java.net.URLEncoder;
import java.util.Date;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.service.UserWebService;
import wenqiang_web.wenqiang_mavenWeb.cn.wenqiang.util.CxfInvokeUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class MyTestClient {
public static void main(String[] args) throws Exception {
String wsdlUrl="http://localhost:8070/wenqiang_mavenWeb/Webservices/lMSWebService?wsdl";
String id="1";
Object[] obs = CxfInvokeUtil.invoke(wsdlUrl,"selectByPrimaryKey",id);
if(obs != null && obs.length > 0){
String result = (String)obs[0];
System.out.print(result);
}
如有不懂 可以和 本人联系 QQ:344436738
- java调用CXF WebService接口的两种方式
通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...
- [转]Net 下采用GET/POST/SOAP方式动态调用WebService C#实现
本文转自:http://www.cnblogs.com/splendidme/archive/2011/10/05/2199501.html 一直以来,我们都为动态调用WebService方法而烦恼. ...
- .Net 下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox
一直以来,我都为动态调用WebService方法而烦恼.在.Net环境下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,但当xmlns改变时就会出 ...
- C# WebService动态调用
前言 站在开发者的角度,WebService 技术确实是不再“时髦”.甚至很多人会说,我们不再用它.当然,为了使软件可以更简洁,更有层次,更易于实现缓存等机制,我是非常建议将 SOAP 转为 REST ...
- 使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。
分布式微服务现在成为了很多公司架构首先项,据我了解,很多java公司架构都是 Maven+Dubbo+Zookeeper基础上扩展的. Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按 ...
- webService动态调用及返回至处理
http://www.cnblogs.com/xffy1028/archive/2012/05/07/2487595.html using System; using System.Collectio ...
- Javassist注解(Annotation)的使用:CXF WebService动态生成
设计一个对接系统,通过动态模型的增删改触发业务系统相应服务的调用.模型增删改方法动态发布为WebService服务.WebService服务采用CXF发布,动态类生成采用Javassist.由于Web ...
- 使用浏览器地址栏调用CXF Webservice的写法
/* * 通过url调用 * http://localhost:8080/EFP/webService/TestWebservice/testOut/arg0/liuyx */ http://loca ...
- cxf客户端动态调用空指针异常
异常信息如下: 二月 , :: 上午 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames 信息: Created classes: ...
随机推荐
- 详解meta标签
Meta标签详解,在网上转的,希望对大家有用 引言 您的个人网站即使做得再精彩,在"浩瀚如海"的网络空间中,也如一叶扁舟不易为人发现,如何推广个人网站,人们首先想到的方法无外乎以下 ...
- opencv与VS的配置
1.VS2015下配置Opencv3.2教程:http://jingyan.baidu.com/article/4b52d702b3209afc5c774b3c.html http://blog.cs ...
- 用 Visual Studio Code 调试 Node.js
环境: Visual Studio Code Node.js 1. 关闭运行中的程序 2.打开入口文件,我这里的入口文件为 app.js 3.点击左侧菜单栏的 debug 按钮 4.点击运行按钮 5 ...
- go-common-pool设计原理分析
common-pool: 对于一些对象的频繁创建会带来很大的系统开销,并且需要对对象数量进行控制来降低资源消耗,比如数据库连接,线程等 common-pool采用了缓存思想来解决这个问题,预先把一些对 ...
- 最短路径之Dijkstras算法(图片格式)
- VMware Ubuntu16.04虚拟机安装MATLAB R2016b
因为这学期上的模式识别课程需要在Linux环境下使用Matlab,所以就在Windows10主机上的Vmware Workstation上的Ubuntu虚拟机上安装了最新版本的MATLAB. 环境: ...
- windows修改Host后未生效。
打开CMD命令,输入ipconfig /flushdns即可
- 将基因组数据分类并写出文件,python,awk,R data.table速度PK
由于基因组数据过大,想进一步用R语言处理担心系统内存不够,因此想着将文件按染色体拆分,发现python,awk,R 语言都能够非常简单快捷的实现,那么速度是否有差距呢,因此在跑几个50G的大文件之前, ...
- 老李推荐:第4章3节《MonkeyRunner源码剖析》ADB协议及服务: ADB协议概览
老李推荐:第4章3节<MonkeyRunner源码剖析>ADB协议及服务: ADB协议概览 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试, ...
- 在centOS7.2里安装virtualenv和flask
1) 安装pip工具 #wget https://bootstrap.pypa.io/get-pip.py #python get-pip.py 2) 安装virtualenv,并创建一个开发环境 # ...