首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑

2012-11-05 
【转】Apache CXF入门范例以及对传递ListMap类型的疑惑?package cfx.serverimport javax.jws.WebMethodi

【转】Apache CXF入门范例以及对传递List<Map>类型的疑惑
?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑

    package cfx.server;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface HelloService {@WebMethodString sayHi(@WebParam String name);}

    ?实现类HelloServiceImpl.java

    ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
      public class HelloServiceImpl implements HelloService {public String sayHi(String name) {System.out.println("HelloServiceImpl.sayHi called");return "Hello"+name;}

      ? WebService配置文件:cxf-servlet.xml(可放置于WEB-INF目录下)

      ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑

        ?web.xml代码,用于添加CXFServlet这个处理webservice请求的控制器类

        ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
          <?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config></web-app>

          Client端测试代码

          ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
            public class CXF {public static void main(String[] args) {JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.getInInterceptors().add(new LoggingInInterceptor());factory.getOutInterceptors().add(new LoggingOutInterceptor());factory.setServiceClass(HelloService.class);factory.setAddress("http://localhost:8080/cxf/services/hello");HelloService client = (HelloService) factory.create();String reply = client.sayHi("特蕾莎");System.out.println("Server said: " + reply);}

            *****************************************************************************

            ?怎么样,是不是很简单啊!现在再来一个和Spring整合的例子

            注意,Server端和Client端都要通过Spring-bean的方式整合

            Server端现在有四个文件,假设是

            HelloService.java

            HelloServiceImpl.java

            HelloDao.java

            HelloDaoImpl.java

            在HelloServiceImpl中存在一个HelloDao的属性,代码省略如下

            ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
              public class HelloServiceImpl implements HelloService {private HelloDao dao;public String sayHi(String name) {System.out.println("HelloServiceImpl.sayHi called");return dao.getString(name);}}

              ?HelloDaoImpl用于处理持久化,代码省略咯

              需要修改的是配置文件,此时可以这样改

              首先在web.xml里加入Spring监听器

              ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
                <?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config></web-app>

                ?橪銗WEB-INF/cxf-servlet這個忟件可以省略咯

                把一个标准的spring-bean文件放在src下(即classes目录下),要让人一看就知道spring大哥进来咯

                applicationContext.xml

                ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑

                  ?

                  這樣啟動服務器的时候,spring就自动进行bean的注入以及WebService服务的发布了

                  接下来是客户端代码

                  銦爲諟普通Java,所以就简单配一下愙戸端的spring文件了

                  ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑

                    ?CXFClientTest.java

                    ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
                      public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "cfx/client/client-beans.xml" });HelloService client = (HelloService) context.getBean("HelloService");testString(client);}static void testString(HelloService client) {String reply = client.sayHi("特蕾莎");System.out.println("Server said: " + reply);}

                      ?*************************************************************************

                      ?

                      然后是复杂数据类型的问题,经过测试,发觉基本数据类型和List都是没有问题的,我的测试方法包括

                      ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
                        @WebMethodString sayHi(@WebParam String name);@WebMethodList<Integer> getList(@WebParam List<String> strs);@WebMethodList<User> getJavaBean();

                        ?

                        但是传递Map时,就出现问题了,所以参照了user's guide,得到如下解决办法

                        测试某个方法的参数和返回值都是Map类型

                        ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
                          @WebMethod@XmlJavaTypeAdapter(MapAdapter.class)Map<String, String> getMap(@WebParam @XmlJavaTypeAdapter(MapAdapter.class) Map<String, String> map);

                          ?

                          ?

                          MapAdapter是我自己写的用於數據類型轉換的适配器类,代码如下

                          ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
                            public class MapAdapter extends XmlAdapter<MapConvertor, Map<String, Object>> {@Overridepublic MapConvertor marshal(Map<String, Object> map) throws Exception {MapConvertor convertor = new MapConvertor();for(Map.Entry<String, Object> entry:map.entrySet()){MapConvertor.MapEntry e = new MapConvertor.MapEntry(entry);convertor.addEntry(e);}return convertor;}@Overridepublic Map<String, Object> unmarshal(MapConvertor map) throws Exception {Map<String, Object> result = new HashMap<String,Object>();for(MapConvertor.MapEntry e :map.getEntries()){result.put(e.getKey(), e.getValue());}return result;}}

                            ?MapConvertor.java Map格式转换类

                            ?【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑【转】Apache CXF入门实例以及对传递List<Map>类型的疑惑
                              @XmlType(name?=?"MapConvertor") ??@XmlAccessorType(XmlAccessType.FIELD) ??public?class?MapConvertor?{ ?????? ??????private?List<MapEntry>?entries?=?new?ArrayList<MapEntry>(); ?????? ??????public?void?addEntry(MapEntry?entry){ ??????????entries.add(entry); ??????} ?????? ??????public?static?class?MapEntry{ ??????????public?MapEntry()?{ ??????????????super(); ??????????} ??????????public?MapEntry(Map.Entry<String,Object>?entry)?{ ??????????????super(); ??????????????this.key?=?entry.getKey(); ??????????????this.value?=?entry.getValue(); ??????????} ??????????public?MapEntry(String?key,Object?value)?{ ??????????????super(); ??????????????this.key?=?key; ??????????????this.value?=?value; ??????????} ??????????private?String?key; ??????????private?Object?value; ??????????public?String?getKey()?{ ??????????????return?key; ??</ 1 楼 pangchaofu 2011-09-02   不错,学习了。 2 楼 woyuanliulian 2012-04-17   list<Map>  到底应该怎么传才能成功?

热点排行