通过CXF的jaxws:client调用SOAP服务

发布的SOAP服务如前面的文章 CXF+Spring发布SOAP服务
http://blog.csdn.net/littlechenlin/article/details/50857474

项目结构如下:
通过CXF的jaxws:client调用SOAP服务_第1张图片

实际上,这里仅需要服务接口文件com.cc.service.UserService(这个目录里有实体类com.cc.entity.User ,只是为了方便展示调用结果,服务实现类com.cc.service.impl.UserServiceImpl 经测不需要),服务接口文件可通过SoapUI生成

package com.cc.service;

import com.cc.entity.User;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * Created by CHENLIN on 2016/3/14.
 */
@WebService(name = "UserService", targetNamespace = "http://impl.service.cc.com")
public interface UserService {
    @WebMethod(operationName = "getName", action = "urn:GetName")
    public User getName(@WebParam(name="userName") String userName);
}

调用xml文件:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <jaxws:client id="client"
                  serviceClass="com.cc.service.UserService"
                  address="http://localhost:9000/serviceTest/service/userService">
    jaxws:client>
beans>

测试调用:
使用JaxWsProxyFactoryBean调用soap服务时,不需要xml文件,这里的操作就相当于xml文件中jaxws:client的配置内容

import com.cc.entity.User;
import com.cc.service.UserService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {

    public static void main(String[] args) {
        //使用JaxWsProxyFactoryBean调用soap服务,但是这种方式需要需要生成接口文件
        System.out.println("***********JaxWsProxyFactoryBean***********");
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(UserService.class);
        factory.setAddress("http://localhost:9000/serviceTest/service/userService");
        UserService userServiceJaxBean = (UserService)factory.create();
        User userJaxBean = userServiceJaxBean.getName("zhangsan");
        System.out.println(userJaxBean.getTelephone());

        //测试jaxws:client,soap服务client调用
        System.out.println("******************soap jaxws:client**************");
        ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext(
                new String[]{"client.xml"});
        UserService us = (UserService)cxt.getBean("client");
        User user = us.getName("aaa");
        System.out.println(user.getTelephone());

    }
}

调用结果如下:(服务端要开启)
通过CXF的jaxws:client调用SOAP服务_第2张图片

你可能感兴趣的:(rest和soap服务)