Apache CXF One of the actual combat Hello World Web Service

Source: Internet
Author: User
Tags wsdl

This article link: http://blog.csdn.net/kongxx/article/details/7525476

The Apache CXF is now almost the preferred class library for building Web service in the Java domain, and it's really easy to use, and here's a brief introduction to several series of articles.

Of course the first thought was of course the Hello World example. The examples used in this series are based on the project Maven builds, and here is my pom.xml file content

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.googlecode.garbagecan.cxfstudy</groupId> <artifactid& Gt;cxfstudy</artifactid> <packaging>war</packaging> <version>1.0-snapshot</version > <name>cxfstudy Maven webapp</name> <url>http://maven.apache.org</url> &LT;PR
        operties> <cxf.version>2.2.7</cxf.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> &LT;ARTIFACTID&GT;CXF-RT-FR
        Ontend-jaxws</artifactid> <version>${cxf.version}</version> </dependency> <dependency> <grouPid>org.apache.cxf</groupid> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <group
            Id>org.apache.cxf</groupid> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <
            Groupid>org.apache.cxf</groupid> <artifactId>cxf-rt-ws-security</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <group Id>org.apache.cxf</groupid> <artifactId>cxf-rt-ws-policy</artifactId> <ver sion>${cxf.version}</version> </dependency> <dependency> <groupid>o Rg.apache.cxf</groupiD> <artifactId>cxf-bundle-jaxrs</artifactId> <version>${cxf.version}</vers
            ion> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </depe ndency> <dependency> <groupId>org.slf4j</groupId> <artifactid>s lf4j-api</artifactid> <version>1.5.8</version> </dependency> <depen Dency> <groupId>org.slf4j</groupId> <artifactid>slf4j-jdk14</artifactid&gt
            ; <version>1.5.8</version> </dependency> <dependency> <groupid>com Mons-httpclient</groupid> <artifactId>commons-httpclient</artifactId> <versio N>3.0</version> </dependency> <dependency> <groupid>commons-io</groupid&gt
            ; 
        <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactid>junit</artif actid> <version>4.8.1</version> <scope>test</scope> </depend ency> </dependencies> <build> <finalName>cxfstudy</finalName> <
            Resources> <resource> <directory>src/main/resources</directory>
                </resource> <resource> <directory>src/main/java</directory>
                <includes> <include>**</include> </includes> &lT;excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupi
                D>org.mortbay.jetty</groupid> <artifactId>maven-jetty-plugin</artifactId> <configuration> <contextPath>/</contextPath> <connectors
                            > <connector implementation= "Org.mortbay.jetty.nio.SelectChannelConnector" > <port>9000</port> </connector> </connector s> </configuration> </plugin> <plugin> <gr Oupid>org.apache.maven.plugins</groupid> &LT;ARTIFACTID&GT;MAVEN-COMPILER-PLUGIN&LT;/ARTIFACTID&G
                T <cOnfiguration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </proj Ect>
Here's a look at HelloWorld's specific examples.

1. Create HelloWorld Interface class

Package Com.googlecode.garbagecan.cxfstudy.helloworld;

Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;

@WebService Public
interface HelloWorld {
    @WebMethod
    @WebResult string Sayhi (@WebParam string text);
2. Create HelloWorld Implementation class

Package Com.googlecode.garbagecan.cxfstudy.helloworld;

public class Helloworldimpl implements HelloWorld {public

    string Sayhi (string name) {
        string msg = "Hello" + N Ame + "!";
        return msg;
    }
}
3. Create the server-side test class

Package Com.googlecode.garbagecan.cxfstudy.helloworld;

Import Org.apache.cxf.jaxws.JaxWsServerFactoryBean;

HTTP://LOCALHOST:9000/HELLOWORLD?WSDL public
class Server {public
    static void Main (string[] args) throws Exception {
        Jaxwsserverfactorybean factory = new Jaxwsserverfactorybean ();
        Factory.setserviceclass (helloworldimpl.class);
        
        Factory.setaddress ("Http://localhost:9000/ws/HelloWorld");
        Factory.create ();

        System.out.println ("Server start ...");
        Thread.Sleep (* 1000);
        System.out.println ("Server exit ...");
        System.exit (0);
    }
}
4. Create the client side test class

Package Com.googlecode.garbagecan.cxfstudy.helloworld;

Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {public
    static void Main (string[] args) {
        Jaxwsproxyfactorybean factory = new Jaxwsproxyfacto Rybean ();
        Factory.setserviceclass (helloworld.class);
        Factory.setaddress ("Http://localhost:9000/ws/HelloWorld");
        HelloWorld HelloWorld = (HelloWorld) factory.create ();
        System.out.println (Helloworld.sayhi ("kongxx"));
        System.exit (0);
    }
}
5. Test

Run the server class first to start the Web Services service, and then access the HTTP://LOCALHOST:9000/WS/HELLOWORLD?WSDL address to determine that the Web service started correctly.

Running the client test class prints the message of Hello kongxx! at the command line.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.