-------------------------- 题记
最近朋友公司需要配置一个webservice,之前也没配过服务器端,所以帮他查了查资料,问了问人,自己配了个完整的webservice的应用。
-------------------------- 配置
一、服务器端central server(即需要提供webservice的服务器)
先看目录结构:
1.source
注:其中IWebService的接口和实现类、service的spring bean、spring.xml是必须的。
2.web-inf
注:remote-servlet.xml必须存在于web-inf下,且名字必须为remote-servlet.xml。
关于web.xml中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>central</display-name>
<!-- 启动spring的监听 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 远程服务的中央servlet -->
<servlet>
<servlet-name>remote</servlet-name>
<!-- 使用spring框架的中央servlet -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 该remote-servlet的spring bean必须存在于web-inf目录下 -->
<param-value>/WEB-INF/remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<!-- 拦截所有/remote/*的请求 -->
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
remote-servlet的配置:
<?xml version="1.0" encoding="gbk"?>
<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">
<!-- HttpInvoker服务端配置(name是映射名) -->
<!-- 使用spring框架的webservice的应用类 -->
<bean name="httpMyService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<!-- 服务实现类(name值固定) -->
<property name="service" ref="webService" />
<!-- 服务接口(name值固定) -->
<property name="serviceInterface" value="com.crx.service.IWebServiceService" />
</bean>
<!-- 用户服务(name值固定) -->
<!-- 使用spring框架的webservice的应用类 -->
<bean id="urlMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- name值固定 -->
<property name="mappings">
<props>
<!-- /httpMyService是url部分,httpMyService是映射名 -->
<!-- 如:http://localhost:8080/projectname/remote/httpMyService -->
<prop key="/httpMyService">httpMyService</prop>
</props>
</property>
</bean>
</beans>
spring.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="config/beans/service.xml" />
</beans>
service.xml中的配置:
<?xml version="1.0" encoding="gbk"?>
<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 name="webService" class="com.crx.service.impl.IWebServiceImpl" />
</beans>
服务接口:
package com.crx.service;
public interface IWebServiceService {
String printInfo() throws Exception;
}
服务实现类:
package com.crx.service.impl;
import java.io.Serializable;
import com.crx.service.IWebServiceService;
public class IWebServiceImpl implements IWebServiceService,Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String printInfo() throws Exception {
return "中央服务器";
}
}
注:还需把接口打成一个jar包,此接口要放入本地服务器的/web-inf/lib里面。
二、本地服务器local server(即需要调用webservice的服务器)
目录结构:
1.source
注:其中WebServlet.java、service.xml、spring.xml是必须的。
2.web-inf
关于web.xml中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>local</display-name>
<!-- 启动spring的监听 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 会使用到webservice的servlet -->
<servlet>
<servlet-name>webservice</servlet-name>
<servlet-class>com.crx.web.servlet.WebServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webservice</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/pages/test/webservice.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="config/beans/service.xml" />
</beans>
service.xml的配置:
<?xml version="1.0" encoding="gbk"?>
<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">
<!-- servlet会调用到的服务 -->
<!-- 该服务使用spring框架的代理bean -->
<bean name="mywebService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >
<!-- 中央服务器中的webservice的url访问地址(name值固定) -->
<property name="serviceUrl" value="http://localhost:8080/publicserver/remote/httpMyService" />
<!-- 中央服务器中的webservice的服务接口(name值固定) -->
<property name="serviceInterface" value="com.crx.service.IWebServiceService" />
</bean>
</beans>
package com.crx.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.crx.service.IWebServiceService;
public class WebServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//初始化spring工厂
// ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(req.getSession().getServletContext());
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
try {
//得到服务bean
com.crx.service.IWebServiceService service=(IWebServiceService) context.getBean("mywebService");
//打印验证
System.out.println(service.printInfo());
System.out.println(service == null);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
好了,打完收工。
-------------------------------------- 结
配置好这些东西,便可以在本机上同时启动两个项目,然后访问http://localhost:8080/local/xxx.do,在控制台看打印结果。
结果应该为:
中央服务器
false