java两个项目之间通过webservice调用-源码仅供参考

项目1、本项目为struts2项目所以需要配置配置文件
applicationcontex-cxf.xml配置

<!-- 定义具体实现的 Bean ,这个 Bean 的定义与 Spring 普通的 Bean 定义是一样的 -->
	<bean id="webServiceInterface"
		class="com.git.easyloan.picc.fpdb.piccservice.impl.WebServiceInterfaceImpl" />

	<jaxws:server id="WebServiceInterface"
		serviceClass="com.git.easyloan.picc.fpdb.piccservice.impl.WebServiceInterfaceImpl"
		address="/WebServiceInterface">
		<!-- 要暴露的 bean 的引用,上面定义的bean id -->
		<jaxws:serviceBean>
			<ref bean="webServiceInterface" />
		</jaxws:serviceBean>
	</jaxws:server>

接口:

@WebService
public interface WebServiceInterfaceIf {

	public String sayHello(@WebParam(name = "arg0") String text);
	
}

实现类:

@Controller
public class WebServiceInterfaceImpl implements WebServiceInterfaceIf{

    public String sayHello(String text) {

        System.out.println("***************1");
        return "Hello : " + text;
    }
    
}

项目2、
实现类:

import javax.xml.namespace.QName;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Service;
@Service("tbCsmCustomerService")
public class TbCsmCustomerServiceImpl implements TbCsmCustomerServiceIf{
	
	@Override
	public Object selectTbCsmCustomer() {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		// 调用项目1的地址
        org.apache.cxf.endpoint.Client client = dcf .createClient("http://localhost:8080/easyloan/ws/WebServiceInterface?wsdl");
        // url为调用webService的wsdl地址
        QName name = new QName("http://service.piccservice.fpdb.picc.easyloan.git.com/", "sayHello");
        // namespace是命名空间,methodName是方法名
        String xmlStr = "1";
        // paramvalue为参数值
        Object[] objects;
        try {
            objects = client.invoke(name,xmlStr);
            System.out.println(objects[0].toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
	}
}

项目2通过webservice调用项目1。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值