webservice-axis2,cfx

axis2是1.6.2的版本,cfx是2.7.5的版本,从apache官网上下载的,编译器是eclipse,操作系统是mac。

最近刚好有用web service,以前都是调用,这次需要发布。

对于如何生成一个web service,网上很多例子,大体:

下载 axis2 bin1.6.2 -> 在eclipse中配置axis2环境 -> 创建项目 -> 创建service java文件 -> 右键点击create web service, 或者手动配置 -> 启动服务器。

 cfx差不多:可以参考,http://www.cnblogs.com/frankliiu-java/articles/1641949.html (中间步骤可以用右键点击create web service代替,不要忘记加在cfx xml文件,刚开始总是404,最后发现是cfx xml的address大小写问题)

调用web service:

可以使用wsimport命令或者axis2/cfx bin 中的bin目录下的wsdl2java命令生成客户端文件,实在比较牛x,可以使用callWebService.

关于传递参数和返回值类型的问题,做了很多试验后总结:

对于用axis2框架的webservice

1: 普通的class 或基本类型没问题。

2: list的问题,

我用axis wsdl2java生成的文件只有stub和一个handle文件,所有的类型和函数都在这stub中,方法返回值list被改为了数组,同样,如果服务器方法参数是list,客户端生成的java代码方法参数也会变成数组。

而且,如果在客户端你传递的参数为数组,假设length为2,服务器端会收到值,服务器端方法的参数值list size也会是2,但是里面的对象属性值会为空!!!所以服务器方法参数必须为数组,客户端传递参数为数组就ok。

如果返回值是一个自定义class type,其中有一个list data,可能也是自定义class,会被转换成object,忘了是wsdl2java还是wsimport,连list都会变为object

WebServiceForUserStub ws = new WebServiceForUserStub();
//方法
WebServiceForUserStub.QueryUser qu = new WebServiceForUserStub.QueryUser();
//调用,得到返回值
User[] us = ws.queryUser(qu).get_return();

//在server端的方法
public List<User> queryUser() {
//
}

我用wsimport生成的文件就有很多了,User都是一个java文件,生成的java 代码中方法queryUser返回的参数居然是list,值倒是对的,就是id换类型了。~~参数也是, 但是传递过来的参数数量不对,值也不对。

//server端
public List<User> queryUser() {
//
}
     
public String setUser(List<User> users) {
//
}

//客户端
 /**
     * 
     * @return
     *     returns java.util.List<com.sf.ws.xsd.User>
     */
    @WebMethod(action = "urn:queryUser")
    @WebResult(targetNamespace = "http://ws.sf.com")
    @RequestWrapper(localName = "queryUser", targetNamespace = "http://ws.sf.com", className = "com.sf.ws.QueryUser")
    @ResponseWrapper(localName = "queryUserResponse", targetNamespace = "http://ws.sf.com", className = "com.sf.ws.QueryUserResponse")
    public List<User> queryUser();

    /**
     * 
     * @param users
     * @return
     *     returns java.lang.String
     */
    @WebMethod(action = "urn:setUser")
    @WebResult(targetNamespace = "http://ws.sf.com")
    @RequestWrapper(localName = "setUser", targetNamespace = "http://ws.sf.com", className = "com.sf.ws.SetUser")
    @ResponseWrapper(localName = "setUserResponse", targetNamespace = "http://ws.sf.com", className = "com.sf.ws.SetUserResponse")
    public String setUser(
        @WebParam(name = "users", targetNamespace = "http://ws.sf.com")
        List<User> users);

找到这么一段话:

 JAX-WS is a specification for implementing web services in Java. It describes how WSDL artifacts can be mapped to Java classes and how this mapping can be applied using annotations. You can download the specification here. The tool wsimport is part of the reference implementation of this specification and the reference implementation is part of the Java class library. There are several alternative implementations, such as Axis2, CXF or Metro, that enhance the basic JAX-WS support with support for additional standards such as WS-ReliableMessaging or WS-Security.

wsimport 和 axis2的区别,后者是增强版。

 cfx

完全ok,参数或返回值是list也是ok的,类型不会被转换,无论是class中的基本类型,或者是class中的list中的类型,都是ok的。

传递参数过去也是ok的。

========以上是我做的测试,如果因为操作问题或者知识不足导致误人子弟,在此表示道歉, simimasi!===========

原文地址:https://www.cnblogs.com/zengyou/p/3159095.html