近期项目间隙 自学了 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: ...
随机推荐
- 在VM中给Linux安装Tool
1.导入tool 2.解压tool 3.打开终端,进入tool的目录,输入 ./XXXXXXX.pl 4.进入安装界面,不断回车即可
- 读learning spark lighting chapter1~chapter2
chapter 1 introduction to the analysis with spark the conponents of Sparks spark core(contains the b ...
- shell 之时间戳
vim 1.sh #/bin/bash##by cc read -p "Please input yourtime:" timea=$timeif [ $a != 0 ] then ...
- 浅谈隐语义模型和非负矩阵分解NMF
本文从基础介绍隐语义模型和NMF. 隐语义模型 ”隐语义模型“常常在推荐系统和文本分类中遇到,最初来源于IR领域的LSA(Latent Semantic Analysis),举两个case加快理解. ...
- WebService从服务端到客户端的用例
1.首先编写Wsdl(基于契约优先的方式),要注意的是命名空间(若是使用include或import)最好使用一致的,代码如下: <?xml version="1.0" en ...
- Tcl与Design Compiler (六)——基本的时序路径约束
本文属于原创手打(有参考文献),如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/ ,作者:IC_learner 时序约束可以很复杂,这 ...
- TypeScript入门-接口
▓▓▓▓▓▓ 大致介绍 在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约. ▓▓▓▓▓▓ 接口 例子: function printLabel(labelledO ...
- 在亚马逊Red Hat 7.1 linux上安装mysql
安装前检查之前是否安装并卸载之前的和删除关联文件 rpm -qa|grep mysql yum remove mysql mysql-server mysql-libs mysql-com ...
- STM32驱动OV7725摄像头颜色识别
实验目的: 使用stm32驱动OV7725摄像头进行图像实时采集,在tft屏幕上实时显示并识别图像中的特定颜色,在颜色的周围画上框. 实验现象: 我的工程代码链接: http://download.c ...
- [UWP]了解模板化控件(4):TemplatePart
1. TemplatePart TemplatePart(部件)是指ControlTemplate中的命名元素.控件逻辑预期这些部分存在于ControlTemplate中,并且使用protected ...