微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cxf学习笔记小小demo

开始学习web service 了。起初接触webservice 是在刚毕业的找工作的时候,遇到一老乡,推荐的技术。可是面试执行董事的时候,被卡掉了。可惜的和老乡的缘分就那么结束了。人都有那么个闪亮的机会,求职的时候遇到老乡,那肯定放你进来,可是哥们我比较衰,被卡死了。好吧,开始学习了。

第一、先大概侃侃这个由来。

web service 的标准出来其实已经很久了,大概2000年的时候,未来估计火的restful就出来了。对于这个websrvice,我只关注了apache的两个项目,那就是axis2和CXF,这两位兄弟。对于学java语言的我来说,果断选择了CXF作为研究方向,不解释。

第二、其实不管学习什么,官方都有比较不错的demo. 这个学习笔记不如说就是拷贝官方的demo.

好了,接下来上代码。解读。

jax-ws这是一个web service标准。java语言实现了这个标准。(题外话,xfire是java自己定的webservice标准,而cxf是由xfire和别的项目演化而来的。)所以,我们使用web service 只要调用java实现好的接口行了。javax.xml.ws 这个开头的,差不多都是。

来,发布一个服务。

对了,环境的搭建主要是引入cfx的包。在官网上写的很清楚。这些包在那里找呢?  下载cxf发布包,lib里面有。截图一张吧。

第一,写一个接口。


@WebService         //anotation 说明这是web service
public interface HelloWorld {

    String sayHi(String text);


    /* Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
     * support interfaces directly.  Special XmlAdapter classes need to
     * be written to handle them
     */
    String sayHiToUser(User user);


    /* Map passing
     * JAXB also does not support Maps.  It handles Lists great,but Maps are
     * not supported directly.  They also require use of a XmlAdapter to map
     * the maps into beans that JAXB can use.
     */
    @XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
    Map<Integer,User> getUsers();
}
// END SNIPPET: service


第二、写实现类。


@WebService(endpointInterface = "server.HelloWorld",
            serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    Map<Integer,User> users = new LinkedHashMap<Integer,User>();


    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }

    public String sayHiToUser(User user) {
        System.out.println("sayHiToUser called");
        users.put(users.size() + 1,user);
        return "Hello "  + user.getName();
    }

    public Map<Integer,User> getUsers() {
        System.out.println("getUsers called");
        return users;
    }

}
// END SNIPPET: service

第三、发布一个服务到 jetty.

public class Server {

    protected Server() throws Exception {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:9000/helloWorld";
        Endpoint.publish(address,implementor);
        // END SNIPPET: publish
    }

    public static void main(String args[]) throws Exception {
        new Server();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}
第四、写一个客户端,测试一下,掉用一下我们刚才写的服务。

    public static void main(String[] args) {  
        //创建WebService客户端代理工厂     
        JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();     
        //注册WebService接口     
        factory.setServiceClass(HelloWorld.class);     
        //设置WebService地址     
        factory.setAddress("http://localhost:9000/helloWorld");          
        HelloWorld iHelloWorld = (HelloWorld)factory.create();     
        System.out.println("invoke webservice...");     
        Map<Integer,User> hellos = iHelloWorld.getUsers();
        for(int i=0;i<hellos.size();i++){  
            System.out.println(hellos.get(i));  
        }  
        System.exit(0);     
    }

好了,cxf 我们就体验完毕了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐