WebService具体是干什么的我这里就不用说了,这里介绍一下Aapche CXF配制与使用,这里我是采用Tomcat for Spring方式WebService服务跟着Tomcat一同启动,先下载CXF所需的包,放到你的工程的WEB-INF/lib目录下,新建bean.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.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" />
<bean id="CXFImpl" class="CXF.CXFServiceDao" />
<jaxws:endpoint
id="ICXFService" implementor="#CXFImpl" address="/ICXFService" />
</beans>
然后web.xml中加入如下内容:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
重启Tomcat即可启动服务。
上面内容中的ICXFService是一个接口文件,代码如下:
package CXF;
import java.util.List;
import java.util.Map;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@WebService
public interface ICXFService {
// 根据缴费周期,用户编号提取当月的记录
@XmlJavaTypeAdapter(CXF.adapter.MapAdapter.class)
Map<String,String> showMap(@WebParam(name = "feeMonth") String feeMonth,@WebParam(name = "userId") String userId);
//根据用户表号查询近6个月的欠费信息
public List<PowerFeeDetailDto> getPowerFeeDetail(@WebParam(name = "userId") String userId);
//根据缴费周期,用户编号提取用户表码信息
@XmlJavaTypeAdapter(CXF.adapter.MapAdapter.class)
Map<String,String> showMapGr(@WebParam(name = "feeMonth") String feeMonth,@WebParam(name = "userId") String userId);
}
CXFService是一个ICXFServicer接口的具体实现,此处就不再列出。
MapAdapter.class是一个拦截器,将查询数据转换成Map格式返回过来。代码如下:
package CXF.adapter;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class MapAdapter extends XmlAdapter<MapPair[],Map<Object, Object>>{
public Map<Object, Object> unmarshal(MapPair[] pairs) {
Map<Object,Object> map = new HashMap<Object, Object>();
for (MapPair pair : pairs) {
map.put(pair.key, pair.value);
}
return map;
}
public MapPair[] marshal(Map<Object, Object> v) throws Exception {
MapPair[] pairs = new MapPair[v.size()];
int i = 0;
for(Map.Entry<Object, Object> entry : v.entrySet()) {
pairs[i++] = new MapPair(entry.getKey().toString(), entry.getValue().toString());
}
return pairs;
}
}
-----------------------------------------------------------------------------------------------------------
MapPair.class
package CXF.adapter;
public class MapPair {
public String key;
public String value;
public MapPair(){
}
public MapPair(String key, String value) {
this.key = key;
this.value = value;
}
}
客户端需要建立一个client-bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 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-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:8080/CXF/HelloWorld"/>
</bean>
</beans>
客户端程序:
package demo.spring.client;
import java.util.Map;
import demo.spring.HelloWorld;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Client {
private Client() {
}
public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});
HelloWorld client = (HelloWorld)context.getBean("client");
System.out.println("Invoke showMap()....");
Map<String, String> map = client.showMap();
if (map !=null){
System.out.println("1:"+map.get("1"));
System.out.println("2:"+map.get("2"));
System.out.println("3:"+map.get("3"));
}
System.exit(0);
}
}