CXF 使用示例

来源:互联网 发布:java .class表示什么 编辑:程序博客网 时间:2024/05/12 07:42

一直对webservice不怎么了解,最近趁闲着的时间在tomcat上下了一个cxf,通过网站等资料做了一个初步示例

在tomcat上下载了cxf,我下载的是apache-cxf-3.1.10。在后续的实践中,这个版本支持的jdk在1.7以上。如果是低于1.7版本的jdk,会报错的

 1.创建了一个名称为cxfService的j2se项目,把cxf下lib中所有jar包都考入到了项目里面(我也不知道哪些包是干什么用的)




User.java的代码,用于后面的应用

package com;
public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


2.HelloWorld.java     webservice接口

package com;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;


@WebService
public interface HelloWorld {
     String sayHi(@WebParam(name="text") String text);
     String sayHiToUser(@WebParam(name="user") User user);
     String[] SayHiToUserList(@WebParam(name="userList") List<User> userList);
 }

@WebService 注释标记 Java 类;实现 Web Service 接口时,标记服务端点接口。

@WebParam: 在@WebService发布成wsdl时候, 方法的参数名称被自动的映射成arg0, arg1, 用@WebParam就可以标记成正常的参数名


3.HelloWorldImpl.java     webservice实现类

package com;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;


@WebService(endpointInterface="com.HelloWorld",serviceName="HelloWorld")

public class HelloWorldImpl implements HelloWorld {
    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
    public String sayHi(String text) {
                return "Hello " + text;
   }

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

   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;
   }

}

从网站上摘抄如下

@WebService   1、serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service。缺省值为 Java 类的简单名称 + Service。(字符串)   2、endpointInterface: 服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口   3、name:此属性的值包含XML Web Service的名称。在默认情况下,该值是实现XML Web Service的类的名称,wsdl:portType 的名称。缺省值为 Java 类或接口的非限定名称。(字符串   4、portName:  wsdl:portName。缺省值为 WebService.name+Port。   5、targetNamespace:指定你想要的名称空间,认是使用接口实现类的包名的反缀   6、wsdlLocation:指定用于定义 Web Service 的 WSDL 文档的 Web 地址。Web 地址可以是相对路径或绝对路径。(字符串)   注意:实现类上可以不添加Webservice注解 

4.WebServiceApp.java  发布


package com;
import javax.xml.ws.Endpoint;
public class WebServiceApp {
    public static void main(String[] args) {
        System.out.println("web service start");
        HelloWorldImpl implementor= new HelloWorldImpl();
        String address="http://localhost:8181/helloWorld";
        Endpoint.publish(address, implementor);
        System.out.println("web service started");
    }
}

在 WebServiceApp.java中点击右键运行


5.在浏览器上输入http://localhost:8181/helloWorld?wsdl



客户端

1.客户端的结构


2.生成服务器段的代码,(service包中的代码是生成出来的)


 把命令转到现在的cxf的bin文件夹下

然后输入下面的命令

  wsdl2java.bat -p com.service -client -encoding utf-8 -noAddressBinding  http://localhost:8181/helloWorld?wsdl

(1).  注意命令行模式切换到 wsdl2java.bat 所在的目录(如果不是, 要写 .bat 所在的全路径)

(2). -p 后面配置的是代码所在的包名

3.HelloWorldClient.java


package com;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.service.HelloWorld;
import com.service.User;

public class HelloWorldClient {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
        svr.setServiceClass(HelloWorld.class);
        svr.setAddress("http://localhost:8181/helloWorld");
        HelloWorld hw = (HelloWorld) svr.create();
        User user = new User();
        user.setName("Tony");
        System.out.println(hw.sayHiToUser(user));
    }
}


控制台输出为:



0 0
原创粉丝点击