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

1、WebService 服务端文件:
文件组成很简单: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导入到客户端项目)文件如下:
webServiceTest代码如下:
public class WebServiceTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
ICc client = (ICc) context.getBean("client"); //如果webservice接口中出现过自定义类,必须将源码添加到客户端中,否则创建client会报找不到
client.showMessages();
}
}
备注:<span style="color:#FF0000;">客户端的applicationContext.xml内容与服务端一致</span>
4、先发布webservice接口,生成WSDl文件(在浏览器中输入:http://localhost:8080/ypms/services/Cc?wsdl,如下所示:
备注以上内容可能不同,只要能看到自己写的方法就可以了
5、运行客户端代码,结果如下:
总结:
这样webservice调用就成功了, 其实客户端可以通过在Eclipse安装Xfire插件,添加WDSL路径自动生成webservice客户端代码,不过遇到个问题很久没解决如下:
问题:关于带參的方法调用导致成意外元素
如果有遇到过解决了的朋友可以分享下。。。
本文介绍了如何使用Java调用WebService接口,包括服务端接口定义、应用上下文配置、客户端测试代码编写,以及通过Eclipse的Xfire插件生成客户端代码。在实际操作中遇到带参数方法调用的问题,期待解决方案。
721

被折叠的 条评论
为什么被折叠?



