在我们的第一篇中发布web service 通过java code, 这里我们使用另外一种方法(Spring)来发布web service
同样还是先声明个接口 然后实现它
package demo.spring;
import javax.jws.WebService;
@WebService
public interface HelloWorld
{
String sayHi(String text);
}
实现这个接口
package demo.spring;
import javax.jws.WebService;
@WebService(endpointInterface="demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
@Override
public String sayHi(String text)
{
System.out.println("sayHi called");
return "Hello " + text;
}
}
然后编写个xml文件 service-beans.xml来描述这个web service
<?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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="impl" class="demo.spring.HelloWorldImpl"/>
<bean id="factory" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld" />
<property name="address" value="http://localhost:9002/HelloWorld" />
<property name="serviceBean">
<ref bean="impl" />
</property>
</bean>
<bean id="bean" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean" factory-bean="factory" factory-method="create" />
</beans>
然后通过Spring来发布他package demo.spring.server;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Server {
protected Server() throws Exception
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"demo/spring/server/server-beans.xml"});
context.getBean("bean");
System.out.println("Server ready...");
}
public static void main(String[] args) throws Exception
{
new Server();
}
}
运行这个文件, 这个web service 就发布了,http://localhost:9002/HelloWorld?wsdl 在这里可以看到发布的wsdl文件
同样我们在客户端也可以使用spring来测试
client-beans.xml
<?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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="demo.spring.HelloWorld" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld" />
<property name="address" value="http://localhost:9002/HelloWorld" />
</bean>
</beans>
package demo.spring.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.spring.HelloWorld;
public class Client {
public static void main(String[] args) throws Exception
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"demo/spring/client/client-beans.xml"});
HelloWorld client = (HelloWorld) context.getBean("client");
String response = client.sayHi("Joe");
System.out.println("Response: "+response);
System.exit(0);
}
}
运行这个Java文件,可以在命令行上看到
客户端:
Response: Hello Joe
服务器端:
Server ready...
sayHi called
和java code 一样, 我们还可以添加拦截器,分别修改server-beans.xml client-beans.xml这两个文件
<?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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="impl" class="demo.spring.HelloWorldImpl"/>
<bean id="inlog" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean id="factory" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld" />
<property name="address" value="http://localhost:9002/HelloWorld" />
<property name="serviceBean">
<ref bean="impl" />
</property>
<property name="inInterceptors">
<list>
<bean class="demo.spring.server.MyInterceptor" />
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</list>
</property>
<property name="outInterceptors">
<list>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</list>
</property>
</bean>
<bean id="bean" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean" factory-bean="factory" factory-method="create" />
</beans>
其中MyInterceptor是我们自己定义的拦截器 而不是cxf提供的, 我们可以用这中方法实现自己的拦截器
package demo.spring.server;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
public class MyInterceptor extends AbstractPhaseInterceptor
{
public MyInterceptor()
{
super(Phase.RECEIVE);
}
public void handleMessage(Message message) throws Fault
{
System.out.println("this is my first Interceptor extends by AbstractPhaseInterceptor");
}
}
修改client-beans.xml
<?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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="demo.spring.HelloWorld" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld" />
<property name="address" value="http://localhost:9002/HelloWorld" />
<property name="inInterceptors">
<list>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</list>
</property>
<property name="outInterceptors">
<list>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</list>
</property>
</bean>
</beans>
这样我们就添加了 输入 输出的log拦截器 和自定义的拦截器。
分享到:
相关推荐
C#Web Service C#Web Service C#Web Service
XML Web Service 开发XML Web Service 开发XML Web Service 开发XML Web Service 开发XML Web Service 开发最好不要下
1.Web service經典開發文檔! 2.它是一個軟件系統﹐為了支持跨網絡的機器間相相互交互而設計。Web Service服務通常被定義為一組模塊化的API﹐它們可以通過網絡進行調用﹐來執行遠程系統的請求服務。 3.XFire 是 ...
Java Web Service 学习例子,里面有大量用来开发web services的Java程序示例,非常值得学习。 使用到的技术及工具如下: Apache: www.apache.org and xml.apache.org -- Ant 1.4.1 -- SOAP 2.2 -- Tomcat 3.2...
service now web service guide
D6 SOAP_WEB SERVICE 使用delphi来开发 webservice 使用soap
webservice 从表面上看Web service 就是一个应用程序它向外界暴露出一个能够通过Web进行调用的API。这就是说你能 够用编程的方法通过Web来调用这个应用程序。 对Web service 更精确的解释 Web ...
远程抄表监控终端软件设计主要包括数据采集和Web Service的设计和实现,重点是使用gsoap软件包设计了供客户端调用的Web Service,并详细论述了远程抄表监控终端与客户端的通信。设计好的远程抄表监控终端作为一个...
Web Service和Grid Service研究,郭璇,,本文分析并比较了两种主流的Web服务技术,Web Service技术和Grid Service技术。首先介绍Web Service 的系统架构和核心技术,并深入探讨Web Servic
Query as a Web Service 指南
《实战Delphi6/Kylix2/SOAP/Web Service程序设计篇》(李维著)
二、 Web Service服务器端的发布。 2.1 创建java类。 2.2 创建services.xml。 2.3 发布服务。 2.4 在浏览器上测试服务。 三、 Web Service 客户端的创建。 3.1 创建WSDL文件。 3.2 利用WSDL创建stub。 3.3 利用stub...
Web Service编程--用C#.NET开发网络服务(1C 作者:陆昆仑,李旭东,吴缓静 编著 出版社:希望电子 出版时间:2003年04月 第一章 .NET和.NET框架 第二章 C#基本语法 第三章 面向对象的C# 第四章 C#高级...
web service discovery
delphi 6/Kylik2 soap/Web Service程序设计篇下册+代码,非常好的教程。
Android之Web Service实现天气预报查询 利用Ksoap实现Web Service功能,大家自己练习的时候需要用到Ksoap2包,下载地址为 http://download.csdn.net/detail/hander_wei/5713765
Delphi6 SOAP Web Service程序设计篇
Web Service 简单实例,JAVA中的Web服务规范等,完整的客户端于服务端的开发流程。
西北某高等院校研究生的web service课件,英文版的,总共7个ppt,比较简练,适合想快速了解学习web service的学生下载学习,分别是Web Services(ch3.1)、Web Services(ch3.2)、Web Services(ch3.3)、Web Services(ch...
T100 Web Service开发,调试整合重启检查说明(简体版)