Cxf 示例01

 

 

 

package com.test.cxf;

import javax.jws.WebService;

@WebService
public interface ICxfTest {
	public String selectEmpName(String empNo);
}

 

 

 

package com.test.cxf;

import javax.jws.WebService;

import com.test.cxf.biz.ICxfTestBiz;

@WebService(endpointInterface = "com.test.cxf.ICxfTest")
public class CxfTestImpl implements ICxfTest {
	
	private ICxfTestBiz cxfTestBiz;

	public void setCxfTestBiz(ICxfTestBiz cxfTestBiz) {
		this.cxfTestBiz = cxfTestBiz;
	}

	public String selectEmpName(String empNo) {
		return cxfTestBiz.selectEmpName(empNo);
	}
}

 

 

 

package com.test.cxf.biz;

public interface ICxfTestBiz {
	public String selectEmpName(String empNo);
}


package comtest.cxf.biz;

public class CxfTestBizImpl implements ICxfTestBiz {

	public String selectEmpName(String empNo) {
		return "say hello" + "\t" + empNo;
	}
}

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<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">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jaxws:endpoint id="cxfService" implementor="#cxfTestService"
		address="/cxftest" />

	<bean id="cxfTestService" class="com.test.cxf.CxfTestImpl">
		<property name="cxfTestBiz" ref="cxfTestBiz" />
	</bean>
	<bean id="cxfTestBiz" class="com.test.cxf.biz.CxfTestBizImpl">
	</bean>

 

	<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>/ws/*</url-pattern>
	</servlet-mapping>

 

package com.test.cxf;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.tools.java2ws.JavaToWS;

public class CxfMain {
	public static void main(String[] args) {
		//testClient();
		testCxf();
	}

	//测试CXF
	public static void testCxf() {
		String url = "http://127.0.0.1:8080/WebTest/ws/cxftest";
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress(url);
		factory.setServiceClass(ICxfTest.class);
		factory.getInInterceptors().add(new LoggingInInterceptor());
		ICxfTest test = (ICxfTest) factory.create();

		String str = test.selectEmpName("xo");
		System.out.println(str);
	}

	//生成WSDL文件
	public static void genWSDL() {	
		String clazz = "com.test.cxf.ICxfTest";
		String[] params = { "-o", "CxfTestService.wsdl", "-wsdl", clazz };
		JavaToWS.main(params);
	}
	
	//客户端调用
	public static void testClient(){
		 //不依赖服务器端接口来完成调用的,也就是不仅仅能调用Java的接口
		JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
		//Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC
		Client client = clientFactory.createClient("http://localhost:8080/WebTest/ws/cxftest?wsdl");
		Object[] result;
		try {
			result = client.invoke("selectEmpName", "xo");
			System.out.println(result[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值