首页 > 代码库 > XFire调用CXF参数为Null的问题

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

 

XFire调用CXF参数为Null的问题