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

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);

    }

}
分类:Java 时间:2010-10-03 人气:219
分享到:
blog comments powered by Disqus

相关文章

  • [Change] using Spring and Apache CXF POJO Web services design and implementation, Part 1: Using CXF and Spring Create Web Services 2010-03-31

    September 18, 2008 Using the open source Apache CXF Web Services Framework can easily create traditional Java ™ objects (plain old Java object, POJO)-style Web services. This article is Part 1 of this series, will show you how to use Spring and CXF P

  • Apache CXF + Spring 2011-09-28

    比较了Spring WS和Apache CXF,觉得Spring WS属于contract-first类型的WebService,自己不熟悉xsd,所以使用Apache CXF这个组件,而且它的功能更为强大并且可以与Spring整合. 部署在tomcat上,然后访问 http://127.0.0.1:8080/foo/HelloWorld?wsdl applicationContext.xml <?xml version="1.0" encoding="UTF-8&qu

  • Apache CXF之初探 2014-08-20

    一.到官网 http://cxf.apache.org/download.html 下载对应的 包. 二.新建Java 工程,把对应的jar包放入去. 三.创建 server 端的程序. 共有3个Java文件: 3.1 新建接口 HelloWorld,此接口只有一个方法,如下代码: package com.yao.cxf.server; public interface HelloWorld { String sayHi(String text); } 3.2 实现接口类 HelloWorldI

  • Simple example of Apache CXF WebService 2010-12-06

    Source Download: http://dl.dbank.com/c0vhvbrxlr CSDN article from my blog, reproduced, please credit: http://blog.csdn.net/cuiran/archive/2010/12/02/6049645.aspx One. Development Environment My development environment is: MyEclipse 6.5 Tomcat 6.0 Apa

  • Apache CXF 调用出现的问题 2013-05-02

    首先申明我用 apache CXF 版本是 apache-cxf-2.5.10. 原文引用:http://blog.csdn.net/crazycoder2010/article/details/6673982 codeweblog.com转载:http://www.codeweblog.com/question/54100_26065 今天突然想试试之前一直没有心情尝试的Apache CXF,就在codeweblog.com上找示例,结果发现一篇很好的文章. 见上. 我采用他写的代码,WebS

  • Apache CXF 整合 RESTful Services 2014-08-22

    一.首先还是创建个项目并把包导入去,弄2个package,分别用于装server和client.记得要把commons-httpclient-3.1.jar也要放入来,因为Client用http请求的: 二.在com.yao.server下新建一个类Customer,我们注意到有一个@XmlRootElement(name="Customer")这个是用来标识Customer是可用来转化为xml的: package com.yao.rs.server; import javax.xml

  • Apache CXF 2013-03-21

    Apache CXF = Celtix + XFire,提供了对 JAX-WS全面的支持,并且提供了多种 Binding .DataBinding.Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用. CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架

  • Spring和Apache CXF,ActiveMQ整合记录 2014-04-12

    Spring 3.2.8 Apache CXF 1.6 ActiveMQ 5.9.0 Tomcat 8.0.3 MyEclipse 2013 1. Spring需要的jar包: spring-aop(aop支持) spring-beans(配置spring bean必须的包) spring-context(应用上下文支持,必须) spring-core(spring核心的包,包括io操作,cglib等) spring-experssion(可能是xml解析相关的包,不要的话XmlWebAppli

  • Public key certificate - a public key encryption 2010-05-28

    Reprinted from: http://www.hoposoft.com/secuinfo/104.html Public key certificate - a public key encryption Public key certificate, usually referred to as the certificate is a digital signature of the declaration, it will bind public key values to the

  • Data encryption and public key private key public key certification 2011-01-25

    I have previously written an article entitled " Web login authentication security design "may be clear enough about, one reader questioned the message that" public disclosure of the name suggests is the matter, as long as you like, no one w

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 1.016 (s). 12 q(s)