近期项目间隙 自学了 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: ...
随机推荐
- Spring使用注解进行事务的管理
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/ ...
- oracle配置监听图形界面不出来解决方法
ROOT用户下,执行 xhost + 然后再切换到oracle用户运行netca DISPLAY 在Linux/Unix类操作系统上, DISPLAY用来设置将图形显示到何处. 直接登陆图形界面或 ...
- C# 通过反射实现类似MVC路由的机制
最近封装了个功能非常类似于MVC的路由.//MVC路由机制先找到Controller Action 什么是反射 反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获 得.NET ...
- 使用Struts2校验器
今天遇到了这样的问题:我的jsp页面.web.xml.struts.xml.UserAction-validation.xml等内容写的都正确,就是无法使用校验器!在网上找了半天就是不出来,迫不得已我 ...
- trimpath javascript的学习
TrimPath是javascript模板引擎. 这几天有一个项目涉及到trimpath这个框架,所以就花了一点时间研究了一下,这个框架和别的javascript框架不太一样的地方就是模板的概念,就是 ...
- dispaly属性,position属性
position:absolute;绝对定位相对于父元素(父元素设为relative) position:relative;相对定位相对于自己 position:fixed;固定定位相对于浏览器 di ...
- es6中的let声明变量与es5中的var声明变量的区别,局部变量与全局变量
自己通过看typescript官方文档里的let声明,与阮一峰老师翻译的的es6学习文档,总结以下三点 1.var声明可以多次重复声明同一个变量,let不行 2.let变量只在块级作用域里面有效果,v ...
- Plupload上传插件简单整理
Plupload Plupload是有TinyMCE的开发者开发的,为您的内容管理系统或是类似上传程序提供一个高度可用的上传插件.Plupload 目前分为一个核心API 和一个jQuery上传队列部 ...
- 【C++】指针与引用的区别
本文主要总结在C++中指针与引用的区别. 从定义与性质来看指针与引用有如下区别: 指针表示的是一块变量的地址 引用表示一个变量的别名. 因此指针变量需要占用空间(一个指针变量在32位系统下占用4字节, ...
- MongoDB--在windows下的安装过程及基本配置
这几天在做一个简单的后台博客系统,数据库用到了 MongoDB ,虽说官方的文档比较全,但是对于我一个英语一般的人来说,或多或少在配置的时候出现了一些问题,总结了一下在安装及创建服务的过程 Mongo ...