• webService cxf学习


    1、首先去官网下载cxf包

    http://archive.apache.org/dist/cxf/

    记住要选.zip结尾 大概40兆的样子

    2、把上边的包都放项目里。如果你用的jeecg框架,那它自带,不过少了一个jetty的包。记得在pom.xml中加入

    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>${cxf.version}</version>
    </dependency>

    3、http://blog.csdn.net/oneGeng/article/details/5979442 这里面讲的很详细
    第一步:新建一个webservice接口 
    Java代码 
    1. @WebService  
    2. public interface IHelloWorld {  
    3.     //@WebParam给参数命名,提高可代码可读性。此项可选  
    4. blic String sayHi(@WebParam(name="text") String text);  
    5. }  

       通过注解@WebService申明为webservice接口 
       第二步,实现WebService接口 
    Java代码 
    1.   @WebService  
    2.   public class HelloWorldImpl implements IHelloWorld {  
    3.   
    4. public String sayHi(String name) {  
    5.     System.out.println("sayHello is called by " + name);  
    6.     return "Hello " + name;  
    7. }  
    8.   
    9.    }  

       第三步,创建服务端 
    Java代码 
    1.   public class Server {  
    2.   
    3. private Server(){  
    4.     IHelloWorld helloWorld = new HelloWorldImpl();  
    5.     //创建WebService服务工厂  
    6.     JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
    7.     //注册WebService接口  
    8.     factory.setServiceClass(IHelloWorld.class);  
    9.     //发布接口  
    10.     factory.setAddress("http://localhost:9000/HelloWorld");  
    11.     factory.setServiceBean(helloWorld);  
    12.     //创建WebService  
    13.     factory.create();  
    14. };  
    15.   
    16. public static void main(String[] args) throws InterruptedException{  
    17.        //启动服务端  
    18.               new Server();  
    19.     System.out.println("Server ready...");  
    20.     //休眠一分钟,便于测试  
    21.                Thread.sleep(1000*60);  
    22.     System.out.println("Server exit...");  
    23.     System.exit(0);  
    24. }  
    25.    }  

        第四步,创建客户端 
      
    Java代码 
    1.    public class Client {  
    2.   
    3. private Client(){};  
    4.   
    5. public static void main(String[] args){  
    6.     //创建WebService客户端代理工厂  
    7.     JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
    8.     //注册WebService接口  
    9.     factory.setServiceClass(HelloWorld.class);  
    10.     //设置WebService地址  
    11.     factory.setAddress("http://localhost:9000/HelloWorld");       
    12.     IHelloWorld iHelloWorld = (IHelloWorld)factory.create();  
    13.     System.out.println("invoke webservice...");  
    14.     System.out.println("message context is:"+iHelloWorld.sayHi("    
    15.                  Josen"));  
    16.     System.exit(0);  
    17. }  
    18.    }  

    4、 首先,运行服务端程序 
        其次,打开浏览器,在地址栏中输入http://localhost:9000/HelloWorld?wsdl

    5、当你运行服务端时,其他原有的程序报错了。比如:javax.servlet-api 读不到了 ,读了geronimo-servlet包中的javax-servlet。。

    在pom.xml里换个位置就行了。程序喜欢读前边的那个。(纠结好久TT)

  • 相关阅读:
    北风网第一季度菜单6
    北风网微信第一季菜单5
    win7卸载打印机驱动
    myeclipse 10激活,本人已测试过可行
    北风网视频菜单4
    Code Project精彩系列(1)
    Code Project精彩系列(1)
    Code Project精彩系列(1)
    实现Windows和Linux之间的文件共享
    实现Windows和Linux之间的文件共享
  • 原文地址:https://www.cnblogs.com/xlj227/p/7235267.html
Copyright © 2020-2023  润新知