webservice cxf 实例

2020-12-13 03:13

阅读:643

1.       新建一个JavaWebProject,命名为cxfDemo
soscw.com,搜素材
选择【next】,为project添加userLib库。
       
soscw.com,搜素材

2.       打开新建的project,在src下新建包com.handou.cxf.test。在包中新建inteface名称为HelloWorld
代码如下:
@WebService
public interface HelloWorld {
    @WebMethod
    public String sayHello(@WebParam(name="name")String name);

}

新建一个class为该接口的实现类,其代码如下:
public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String name) {
       // TODO Auto-generated method stub
       return name.concat(" , Hello WebService!!!");
    }
}

3.       发布webservice。
采用cxf内置的Jetty服务器发布服务。在HelloWorldImpl的实现类中添加main()方法,具体如下:
public static void main(String[] args) {
      HelloWorldImpl implementor = new HelloWorldImpl();
      JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
      svrFactory.setServiceClass(HelloWorld.class);
      svrFactory.setAddress("http://localhost:8080/helloWorld");
      svrFactory.setServiceBean(implementor);
      svrFactory.getInInterceptors().add(new LoggingInInterceptor());
      svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
      svrFactory.create();
   }

4.       在地址栏输入http://localhost:8080/helloWorld?wsdl,查看wsdl详细信息

soscw.com,搜素材


5.       访问webservice服务端。
方法1:由wsdl可知接口只有一个供调用的operation,方法名称为sayHello,输入参数变量名称为name,数据类型为string,采用地址栏url?+参数测试,输入:
http://localhost:8080/helloWorld/sayHello?name=HanDou,显示返回信息如图:
soscw.com,搜素材

方法2:客户端编码调用。
        新建Java类HelloClient,其代码如下:
       public class HelloClient {
    /**
     * @param args
     */
    public static void main(String[] args) {   
       JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
       //factory.getInInterceptors().add(new LoggingInInterceptor());
       factory.getOutInterceptors().add(new LoggingOutInterceptor());
       factory.setServiceClass(HelloWorld.class);
       factory.setAddress("http://localhost:8080/helloWorld");
       HelloWorld client = (HelloWorld) factory.create();
       String reply = client.sayHello("HanDou");
       System.out.println("Server said: " + reply);
       System.exit(0);
    }
}

控制台显示反馈消息:

soscw.com,搜素材

6.       采用spring+cxf集成发布webservice。

1.在WEB-INF下新建beans.xml,beans.xml中添加cxf配置。
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
   
   
   

          id="helloWorld" 
      implementor="com.handou.cxf.test.HelloWorldImpl" 
      address="/HelloWorld"/>

2.在web.xml中添加spring和cxf的配置
      
   
       contextConfigLocation
       WEB-INF/beans.xml
   

   
      
           org.springframework.web.context.ContextLoaderListener
      

   

   
       CXFServlet
       CXF Servlet
      
           org.apache.cxf.transport.servlet.CXFServlet
      

       1
   

   
       CXFServlet
      /*
   

3.部署文件至服务器,启动服务器,通过IP地址+端口+工程名+webservice服务名。 
先访问工程,如图:
soscw.com,搜素材

 

点击wsdl进入http://localhost:8080/cxf/HelloWorld?wsdl,可获得wsdl详细信息

soscw.com,搜素材


评论


亲,登录后才可以留言!