cxf 客户端调用两种方式

第一种原生的调用方式:

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 
        
        Map<String, Object> outProps = new HashMap<String, Object>();
        outProps.put(WSHandlerConstants.ACTION,WSHandlerConstants.USERNAME_TOKEN);
        outProps.put(WSHandlerConstants.USER, "admin");
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,MyClientPasswordCallback.class.getName());
        WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
        factory.getOutInterceptors().add(wssOut);
        // 
        factory.setServiceClass(YourClass.class);
        factory.setAddress("youraddress?wsdl");
        YourService client = (YourService) factory.create();
        try {
            String ss = client.yourMethod("parameters");
            System.out.println(ss);
        } catch(Exception e){
            e.printStackTrace();
        }


第二种 与spring 配合调用

1.在配置文件中配置:

<bean id="clientCallback" class="com.travelsky.adap.re.ras.da.service.test.MyClientPasswordCallback">
<property name="userIdentity">
<map>
<entry key="test" value="test" />
</map>
</property>
</bean>
<bean id="wss4jOut" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
<entry key="user" value="test" />
<entry key="passwordCallbackRef"> <ref bean="clientCallback" />
</entry>
</map>
</constructor-arg>
</bean>

 <jaxws:client id="client"
serviceClass="yourclass" address="youraddress">
<jaxws:outInterceptors>
<ref bean="wss4jOut" />
</jaxws:outInterceptors> 
</jaxws:client>


2.单元测试

private ApplicationContext context;


@Before
public void init()
{
this.context = new ClassPathXmlApplicationContext("classpath*:applicationContext-cxf-client.xml");
}


@Test
public void test() throws InterruptedException, RasException
{
YourService  client=(YourService )context.getBean("client");
long start = System.currentTimeMillis();
      /*设置查询参数*/
        System.out.println("request string:"+req);

//调用方法
        String a = client.clsChangeAnalyzeWs(req);
        System.out.println("the return datas:"+a);
        long end = System.currentTimeMillis();
        System.out.println((end-start)/1000.0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值