上一篇文章中,我们已经讲了如果简单的创建一个webservice接口

http://www.cnblogs.com/snowstar123/p/3395568.html

现在我们创建一个简单客户端接口的调用方式,这里我们介绍一种比较特别的方法,

首先,我们我们通过接口地址来生成服务端的相关代码

下载cxf的源码,解压进入到bin目录下,然后执行

wsdl2java -p service.webservice.client.demo -d D:\temp(这个是生成的文件存放的路径)  -verbose http://localhost/*****?wsdl(这里是接口地址)

如果执行此命令时报错,可以尝试配置一下系统的环境变量CXF_HOME为E:\apache-cxf-2.7.0,PATH中添加E:\apache-cxf-2.7.0\bin,CLASSPATH中添加E:\apache-cxf-2.7.0\lib,然后在执行

执行完成后,到存放的路径下查看,会看到多个java文件,将这些生成的java文件添加到项目中,直接加到项目中会有编译错误,修改一下java的package,如果还有其他错误,直接按照eclipse提示修改即可,如super(wsdlLocation, SERVICE, features);报错按照提示直接改成super(wsdlLocation, SERVICE);

再修改除了ObjectFactory以外的类中@XmlType的name属性值,可修改成任意不重复字符,如

@XmlType(name = "greetingResponse", propOrder = {
"_return"
})

我直接改成了

@XmlType(name = "greetingResponse1", propOrder = {
"_return"
})

接下来,我要介绍的是一个稍微和常见的写法不一样的步骤,主要的目的是为了让此接口变的可配置化

首先,我们创建一个javaBean对象,

@Entity
@Table(name="T_WEBSERVICE_ENDPOINT")
public class EndPointEO{

@Column(name="endPointName")

private String endPointName;

@Column(name="endpointUrl")
private String endpointUrl;

@Column(name="clientClass")
private String clientClass;

此javaBean中包含以下属性,endPointName(接口的短名,随便取一个具有唯一性的名字即可,主要是通过此字段来获取其他属性的),endpointUrl(接口地址),clientClass(这个是上面我们通过wsdl2java命令生成的接口类,如我这边配置的是com.cpic.caf.demo.webservice.client.greet.GreetingService)

然后我创建一个类来查询EndPointEO,

@Repository
public interface EndPointRepository extends Repository<EndPointEO, Long> {

@Query("select e from EndPointEO e where e.endPointName = :endPointName ")
EndPointEO findByEndPointName(@Param("endPointName")String endPointName);
}

现在我们创建一个另外一个类用于将EndPointEO转成我们需要调用的类

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;

import org.apache.cxf.frontend.ClientProxy;
import org.springframework.stereotype.Service;

@Service
public class EndPointWebService {
@Autowired
private EndPointRepository endPointRepository;

public Object getClient(String endPointName) throws Exception{
EndPointEO endPointEO = endPointRepository.findByEndPointName(endPointName);

String url = endPointEO.getEndpointUrl(); 
String className = endPointEO.getClientClass();
String methodPart = className.substring(className.lastIndexOf(".")+1);
String methodName = "get" +methodPart + "ImplPort";
className = className +"ImplService";
Class<?> classN = Class.forName(className);
Constructor<?> cons = classN.getConstructor(java.net.URL.class);
Object object = cons.newInstance(new URL(url));
Method method = object.getClass().getMethod(methodName);
object = method.invoke(object);
//org.apache.cxf.endpoint.Client client = ClientProxy.getClient(object);
//client.getInInterceptors().add(new org.apache.cxf.interceptor.LoggingInInterceptor());
//client.getOutInterceptors().add(new com.cpic.caf.tech.webService.interceptor.MyCxfOutInterceptor());
//client.getOutInterceptors().add(new org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor());

//HashMap<String, Object> map = new HashMap<String, Object>();
//map.put("action", "UsernameToken");
//map.put("user", "ws-client");
//map.put("passwordType", "PasswordText");
//map.put("passwordCallbackClass", "com.cpic.caf.demo.webservice.client.ClientPasswordCallback");
//client.getOutInterceptors().add(new org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor(map) );
return object;
}

}
}

现在我们可以来使用这个类啦

如下,调用时,直接注入

@Autowired

private EndPointWebService endPointWebService;

然后调用

GreetingService greetingService = (GreetingService)endPointWebService.getClient("greetingService");//greetingService为EndPointEO中的短名

大致的功能就是这样啦;

这样处理之后,以后有调用接口的地方,直接在数据库中配置一下,即可使用此接口,如果接口地址有变换,只要在数据库中修改一下接口地址即可!

下一篇,我们再在此基础上增加一些密码校验的功能!现在项目中发布到公网上的接口地址,如果不加用户名,密码校验,很容易就会被人利用!

cxf的使用及安全校验-02创建简单的客户端接口的更多相关文章

  1. cxf的使用及安全校验-01创建简单的服务端接口

    最近因为项目的需要,研究了一下webservice的使用: 这里以cxf2.7.0为例,大致介绍一下,也用于备份啦(张立胜) 大致介绍一下项目的情况:项目有maven管理,webservice调用的方 ...

  2. SpringBoot IntelliJ创建简单的Restful接口

    使用SpringBoot快速建服务,和NodeJS使用express几乎一模一样,主要分为以下: 1.添加和安装依赖  2.添加路由(即接口) 3.对路由事件进行处理 同样坑的地方就是,祖国的防火墙太 ...

  3. 【三石jQuery视频教程】02.创建 FontAwesome 复选框和单选框

    视频地址:http://v.qq.com/page/m/8/c/m0150izlt8c.html 大家好,欢迎来到[三石jQuery视频教程],我是您的老朋友 - 三生石上. 今天,我们要通过基本的H ...

  4. Socket创建简单服务器和客户端程序

    使用Socket编程创建简单服务器和客户端 要知道的 Socket-AddressFamily, SocketType, ProtocolType https://blog.csdn.net/weix ...

  5. [.NET] WebApi 生成帮助文档及顺便自动创建简单的测试工具

    ==========最终的效果图========== ==========下面开始干活:生成帮助文档========== 一.创建 WebApi 项目 二.找到 HelpPageConfig.cs 并 ...

  6. Web Service 的创建简单编码、发布和部署

    最近,老大准备将已有的C/S架构项目中的通信部分做成通用,需要将其支持WebService为以后项目向着B/S架构升级做好铺垫,为此身为屌丝的我去各种百度WebService是个什么卵玩意,然后逐渐搭 ...

  7. Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机

    <Windows Azure Platform 系列文章目录> 本文介绍的是国外的Azure Global.如果是国内由世纪互联运维的Azure China,请参考这篇文档: Azure ...

  8. myeclipse(2015)中创建简单的Maven项目的步骤(用于生成可执行jar文件)------》myeclipse2015

    利用MyEclipse的引导,可以很方便的创建简单的.用于生成可执行jar文件的Maven项目: 1.New -> Project... 选择 Maven Project, 点击Next > ...

  9. 利用委托与Lambada创建和调用webapi接口

    前言 现在项目中用的是webapi,其中有以下问题:    1.接口随着开发的增多逐渐增加相当庞大. 2.接口调用时不好管理. 以上是主要问题,对此就衍生了一个想法: 如果每一个接口都一个配置文件来管 ...

随机推荐

  1. C#生成高清缩略图

    /// <SUMMARY> /// 为图片生成缩略图 /// </SUMMARY> /// <PARAM name="phyPath">原图片的 ...

  2. ORA-02447: cannot defer a constraint that is not deferrable

    一个constraint如果被定义成deferrable那么这个constraints可以在deferred和imediate两种状态相互转换. deferred只在transaction中有效,也就 ...

  3. Mysql启动失败 MYSQL:The server quit without updating PID file

    MySQL5.6启动时出错 提示MYSQL:The server quit without updating PID file 首先执行 /bin/mysqld_safe --user=mysql & ...

  4. CommandBehavior.CloseConnection的使用

    CommandBehavior.CloseConnection的使用 分析问题 由于流模式读取数据库的特点,在具体应用时很难确定数据库连接何时才能被关闭,因为读取的动作是连续进行的,下面是一个常见的数 ...

  5. ocos 信号量

    信号量分为  :声明信号量.互斥信号量 转: ucos-ii学习笔记——信号量的原理   ucos-ii学习笔记——信号量的原理及使用 #include "INCLUDES.h" ...

  6. VC维

    vc理论(Vapnik–Chervonenkis theory )是由 Vladimir Vapnik 和 Alexey Chervonenkis发明的.该理论试图从统计学的角度解释学习的过程.而VC ...

  7. Simple Event Correlation installation and configuration

    http://searchenterpriselinux.techtarget.com/tip/Simple-Event-Correlation-installation-and-configurat ...

  8. cf B. Dima and To-do List

    http://codeforces.com/contest/366/problem/B 从0到k枚举起点,然后i+k判断是不是i+k>=n如果是i=(i+k)%n;否则i=i+k; #inclu ...

  9. BZOJ 2527 Meteors

    http://www.lydsy.com/JudgeOnline/problem.php?id=2527 思路:整体二分 #include<cstdio> #include<cmat ...

  10. github如何删除一个repository(仓库)

    昨天有人问我如何删除自己创建的一个项目,我浏览了一下github网站,确实不太容易找到删除功能.这里介绍一下啊如何在github上删除一个repository 一:首先点击进入你要删除的reposit ...