Using JAX-WS standard Endpoint APIs developed a complete example of WebService

分类:Java 2010-10-03 来源:CodeWeblog.com 人气:217

WebService with CXF to develop very simple, thanks to the hard work the team apache CXF. Here I am to practice the process of CXF to others.

This is the way of programming the full release WebService example, be regarded as entry-level Hello world.

WS server:

(1) HelloWorld.java Interface

package com.xxx.ws.code.server;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
        String sayHi(String text);
}

(2) implementation class HelloWorldImpl.java

package com.xxx.ws.code.server.impl;

import javax.jws.WebService;

import com.xxx.ws.code.server.HelloWorld;

@WebService(endpointInterface = "com.xxx.ws.code.server.HelloWorld",
        serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

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

}

(3) published web service

package com.xxx.ws.code.server;

//import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.xxx.ws.code.server.impl.HelloWorldImpl;

public class RunCXFServer {

    protected RunCXFServer() throws Exception {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        HelloWorldImpl implementor = new HelloWorldImpl();
        JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
        svrFactory.setServiceClass(HelloWorld.class);
        svrFactory.setAddress("http://localhost:8002/helloWorld");
        svrFactory.setServiceBean(implementor);
        svrFactory.getInInterceptors().add(new LoggingInInterceptor());
        svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
        svrFactory.create();

        // END SNIPPET: publish
    }

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

(4) Run RunCXFServer

http://localhost:8002/helloWorld?wsdl can see the output saop envelope.

Preparation of client calls WebService

package com.xxx.ws.code.client;

//import javax.xml.namespace.QName;
//import javax.xml.ws.Service;
//import javax.xml.ws.soap.SOAPBinding;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

import com.xxx.ws.code.server.HelloWorld;

public class RunCXFCodeClient {
        public static void main(String args[]) throws Exception {

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(HelloWorld.class);
        factory.setAddress("http://localhost:8002/helloWorld");
        HelloWorld client = (HelloWorld) factory.create();

        String reply = client.sayHi("HI");
        System.out.println("Server said: " + reply);

    }

}
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.709 (s). 14 q(s)