`
huainansto
  • 浏览: 27561 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

web service

    博客分类:
  • CXF
阅读更多

在我们的第一篇中发布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拦截器 和自定义的拦截器。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics