使用cxf做webservice接口调用

一、服务端

建javaweb工程,去官网下载所需的cxf接口发布的jar包,导入到工程。官网地址:http://cxf.apache.org/download.html

1、建立调用接口

package com.ymx.cxf.server;

import javax.jws.WebService;

@WebService
public interface UserService {

    User getUser(String name);
}
View Code

2、实现接口

package com.ymx.cxf.server;

import javax.jws.WebService;

@WebService
public class UserServiceImpl implements UserService {

    @Override
    public User getUser(String name) {
        User user = new User();
        user.setName(name);
        user.setAge(22);
        return user;
    }

}
View Code

3、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>cxfserver</display-name>
  
  <!-- cxf服务启动servlet -->
  <servlet>
      <servlet-name>CXFServlet</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>CXFServlet</servlet-name>
      <url-pattern>/cxf/*</url-pattern>
  </servlet-mapping>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-cxf.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
View Code

4、配置spring-cxf.xml,发布接口

<beans xmlns="http://www.springframework.org/schema/beans"   
    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">  
        
    <!-- 引入CXF配置文件,低版本还需引入其他两个文件 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    
    <!-- 配置方式    注意:implementor为接口的具体实现类 -->
    <jaxws:endpoint implementor="com.ymx.cxf.server.UserServiceImpl" address="/user" ></jaxws:endpoint>
          
</beans>
View Code

5、使用tomcat发布工程,访问发布的cxf接口的地址

  http://10.50.10.52:8080/cxfserver/cxf/user?wsdl

  cxfserver发布的工程名,cxf为配置在web.xml中的url-pattern里面的值,user为配置在spring-cxf.xml中的address。

二、客户端

1、进入解压后cxf文件下的bin目录,使用wsdl2java命令生成客户端代码。

  

  -p  指定其wsdl的命名空间,也就是要生成代码的包名;
  -d  指定要产生代码所在目录;
  -client 生成客户端测试web service的代码;
2、把生成的客户端代码导入工程内,调用服务端代码如下:
package com.ymx.cxf.client;

import javax.xml.namespace.QName;

/**
 * This class was generated by Apache CXF 2.5.9
 * 2017-05-20T10:27:29.544+08:00
 * Generated source version: 2.5.9
 * 
 */
public final class UserService_UserServiceImplPort_Client {

    private static final QName SERVICE_NAME = new QName("http://server.cxf.ymx.com/", "UserServiceImplService");

    public static void main(String args[]) throws Exception {

        UserServiceImplService ss = new UserServiceImplService(UserServiceImplService.WSDL_LOCATION, SERVICE_NAME);
        UserService port = ss.getUserServiceImplPort();  
        
        String name = "test";
        User result = port.getUser(name);
        System.out.println("getUser.result= " + result.getName() + " : " + result.getAge());
    }
}
View Code

 

posted @ 2017-05-20 10:47  吉良吉影的冒险  阅读(758)  评论(0编辑  收藏  举报