XFire调用CXF参数为Null的问题

最近,领导分配了一个任务,做接口联调。情况是这样,对方客户升级了接口采用CXF,而我们还是用的XFire1.2.6,首先就遇到了这个问题:XFire调用CXF参数为Null的问题 。

在网上搜了一大堆资料:

http://blog.csdn.net/larry_lv/article/details/6721057

http://ks2144634.blog.163.com/blog/static/133585503201412855556210/

按照第一个始终还是没能解决,后来找到第二个花了不少时间也还是未能解决。

最后通过仔细琢磨,在第二个中发现Xfire默认会给映射的参数名称加上命名空间(接口所在包的逆向),尝试将targetNamespace的值改为接口包的逆向终于调试成功。

一、CXF服务服务端代码

接口类HelloWorld.java

package com.hsy.server;

import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.hsy.pojo.User;

@WebService
public interface HelloWorld {

    String sayHi(@WebParam(name="name", targetNamespace= "http://server.hsy.com/") String name);  
    String sayHiToUser(User user);  
    String[] SayHiToUserList(List<User> userList);  
    
}
View Code

实现类HelloWorldImpl.java

package com.hsy.server;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import com.hsy.pojo.User;

@WebService(endpointInterface="com.hsy.server.HelloWorld", serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    Map<Integer, User> users = new LinkedHashMap<Integer,User>();
    
    @Override
    public String sayHi(String name) {
        System.out.println(name);
        return "Hello,"+name;
    }

    @Override
    public String sayHiToUser(User user) {
        users.put(users.size()+1, user);  
        return "Hello,"+user.getName()+","+user.getDescription();
    }

    @Override
    public String[] SayHiToUserList(List<User> userList) {
        String[] result = new String[userList.size()];  
        int i = 0;  
        for(User u:userList){  
            result[i] = "Hello " + u.getName();  
            i++;  
        }  
        return result; 
    }

}
View Code

服务启动类WebServiceTest.java

package com.hsy.server;

import javax.xml.ws.Endpoint;

public class WebServiceTest {

    public static void main(String[] args) {
        
        HelloWorldImpl hw = new HelloWorldImpl();
        String address = "http://localhost:8080/cxf_service";
        Endpoint.publish(address, hw);
        System.out.println("WebService暴露成功。。。");
        
    }
    
}
View Code

 

二、XFire客户端测试代码

Xfire测试类XfireClient.java

package com.xhw;

import java.net.HttpURLConnection;
import java.net.URL;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;


/**
 * @filename XfireClient.java
 * @author xiehongwei
 * @date 2017-6-21 下午4:09:47
 */
public class XfireClient {
    
    int Timeout=300000;//单位是毫秒

    public static void main(String[] args) throws Exception {
        XfireClient xc = new XfireClient();
        String urlStr = "http://localhost:8080/cxf_service/webservice/helloWorld?wsdl";
        String moniterMethod = "sayHi";
        
        Client client = xc.loadClient(urlStr);
        
        String name = "xiehongwei";
        Object[] objs = client.invoke(moniterMethod, new Object[] { name });
        System.out.println("返回值为:"+objs[0]);
    }
    
    public Client loadClient(String urlStr) throws  Exception{
        URL _url = new URL(urlStr);
        HttpURLConnection httpConnection = (HttpURLConnection)_url.openConnection();
        httpConnection.setReadTimeout(Timeout);//设置http连接的读超时,单位是毫秒

        httpConnection.connect();
        Client _client = new Client(httpConnection.getInputStream(), null);
        _client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, String.valueOf( Timeout ));//设置发送的超时限制,单位是毫秒;
        _client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
        _client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
        return _client;
    }
    
}
View Code

 

转载于:https://www.cnblogs.com/xiehongwei/p/7081817.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值