除了使用Spring集成RMI,AXIS来实现Webservice,Spring也提供了一套自身的类WebService 实现,这就是传说中《Spring HTTP Invoker》,下面我们来看如何使用
同样,本文分别服务端和客户端两个projcet
服务端:
服务接口:
package ch16.SimpleHTTP;
public interface HelloWorld {
public String getMessage();
}
服务实现
package ch16.SimpleHTTP;
public class SimpleHelloWorld implements HelloWorld {
public String getMessage() {
return "hello world http";
}
}
使用Spring配置文件发布一个HTTP调用者服务HttpInvoker-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<bean name="/helloWorld" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<bean class="ch16.SimpleHTTP.SimpleHelloWorld"/>
</property>
<property name="serviceInterface">
<value>ch16.SimpleHTTP.HelloWorld</value>
</property>
</bean>
</beans>
这个配置清晰易懂,但需要注意HttpInvokerServiceExporter被指定为/helloWorld,当处理请求时候,Spring的DispatcherServlet使用定义了的HandlerMapping去寻找被定义了路由请求的控制器,在这个例子中,我们定义了一个BeanNameUrlHandlerMapping当作我们唯一的HandlerMapping的实现,顾名思义,BeanNameUrlHandlerMapping将被用来根据进入的请求来映射到不同的URL所以在这个例子中,如果某个进入的请求URI(去掉主机名,上下文路径,servlet映射后)是/helloWorld,BeanNameHandlerMapping将路它到/helloWorld
我们的配置文件是通ContextLoaderServlet加载,默认的defaultHandlerMapping和上配置文件的内容都是由DispatcherServlet加载,所以,对文件的命名非常重要,需要把文件放到WEB-INF目录下,并且命名为<servlet-name-servlet.xm的方式,这里由于web.xml中servle名为httpInvoke,顾命名为httpInvoker-servlet.xml
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/httpInvoker-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>httpInvoker</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>httpInvoker</servlet-name>
<url-pattern>/http/*</url-pattern>
</servlet-mapping>
客户端:
首先把服务端的服务接口打包成jar,放到客户端的classpath下,或直接copy接口文件
客户端配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:81/ProSpringStudyWeb/http/helloWorld</value>
</property>
<property name="serviceInterface">
<value>ch16.SimpleHTTP.HelloWorld</value>
</property>
</bean>
</beans>
测试代码:
package ch16.SimpleHTTP;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ch16/SimpleHTTP/applicationContext.xml");
HelloWorld helloWorld=(HelloWorld)context.getBean("helloWorldService");
System.out.println(helloWorld.getMessage());
}
}
运行客户端后运行测试程序,后台可打印出:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
hello world http