码迷,mamicode.com
首页 > 编程语言 > 详细

Java 调用webservice接口测试

时间:2014-12-04 20:00:15      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:cxf   java   wsdl   webservice   eclipse   

Java环境:Eclipse4.4.1   Jdk1.6   Cxf2.7 

1、WebService 服务端文件:

bubuko.com,布布扣

文件组成很简单:webservice接口ICc  和 接口类实现CcImpl 

      ICc 接口代码如下:

package com.yp.webservice;

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

/**
 * 
 * @author yakcy
 * @version v1.0.0
 * @date 2014-11-7
 * 
 */
@WebService(name="ICc",targetNamespace="http://test.com")
public interface ICc
{
	@WebMethod
	void showMessages();
}
     CcImpl 接口实现代码如下:

package com.yp.webservice;

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

/**
 * 
 * @author yakcy
 * @version v1.0.0
 * @date 2014-11-7
 * 
 */

@WebService(name = "ICc", targetNamespace = "http://test.com", endpointInterface = "com.yp.webservice.ICc")
public class CcImpl implements ICc {
	@WebMethod
	@Override
	public void showMessages() {
		System.out.println("Everthing is OK...");
	}

} 


2、服务端配置:applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
	license agreements. See the NOTICE file distributed with this work for additional 
	information regarding copyright ownership. The ASF licenses this file to 
	you under the Apache License, Version 2.0 (the "License"); you may not use 
	this file except in compliance with the License. You may obtain a copy of 
	the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
	by applicable law or agreed to in writing, software distributed under the 
	License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
	OF ANY KIND, either express or implied. See the License for the specific 
	language governing permissions and limitations under the License. -->
<!-- START SNIPPET: beans -->
<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="Cc" implementor="com.yp.webservice.CcImpl"
		address="/Cc" />  

	<bean id="client" class="com.yp.webservice.ICc" factory-bean="clientFactory"
		factory-method="create" />
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass" value="com.yp.webservice.ICc" />
		<property name="address" value="http://localhost:8080/ypms/services/Cc" />
	</bean>

</beans>
<!-- END SNIPPET: beans -->

3、webservice客户端(新建一个java 项目,将webservice服务端代码打包成jar导入到客户端项目)文件如下:

bubuko.com,布布扣

webServiceTest代码如下:

public class WebServiceTest {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationContext.xml" });
		ICc client = (ICc) context.getBean("client"); <span style="color:#FF0000;">//如果webservice接口中出现过自定义类,必须将源码添加到客户端中,否则创建client会报找不到</span>
		client.showMessages();
	}
}

备注:<span style="color:#FF0000;"><span style="background-color: rgb(255, 255, 255);">客户端的applicationContext.xml内容与服务端一致</span></span>


4、先发布webservice接口,生成WSDl文件(在浏览器中输入:http://localhost:8080/ypms/services/Cc?wsdl,如下所示:

bubuko.com,布布扣

  备注以上内容可能不同,只要能看到自己写的方法就可以了

5、运行客户端代码,结果如下:

bubuko.com,布布扣

总结:

       这样webservice调用就成功了, 其实客户端可以通过在Eclipse安装Xfire插件,添加WDSL路径自动生成webservice客户端代码,不过遇到个问题很久没解决如下:
      bubuko.com,布布扣

问题:关于带參的方法调用导致成意外元素

如果有遇到过解决了的朋友可以分享下。。。




Java 调用webservice接口测试

标签:cxf   java   wsdl   webservice   eclipse   

原文地址:http://blog.csdn.net/cqboy1991/article/details/41727495

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!