Link: http://blog.csdn.net/kongxx/article/details/7525476
Apache's cxf has almost become the preferred class library for Building Web Services in the Java field, and it is indeed easy to use. Below we will give a brief introduction through several series of articles.
Of course, the first thing that comes to mind is the Hello world example. The examples used in this series of articles are Maven-based projects. The following is the content of my pom. xml file.
<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>cxfstudy</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>cxfstudy Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <cxf.version>2.2.7</cxf.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-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> <groupId>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> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>cxfstudy</finalName> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <contextPath>/</contextPath> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>9000</port> </connector> </connectors> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build></project>
Let's take a look at the specific example of helloworld.
1. Create a 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;@WebServicepublic interface HelloWorld { @WebMethod @WebResult String sayHi(@WebParam String text);}
2. Create a helloworld implementation class
package com.googlecode.garbagecan.cxfstudy.helloworld;public class HelloWorldImpl implements HelloWorld { public String sayHi(String name) { String msg = "Hello " + name + "!"; return msg; }}
3. Create a server-side test class
package com.googlecode.garbagecan.cxfstudy.helloworld;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;// http://localhost:9000/HelloWorld?wsdlpublic 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(60 * 1000); System.out.println("Server exit..."); System.exit(0); }}
4. Create a client 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 JaxWsProxyFactoryBean(); 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
First, run the server class to start the web service, and then access http: // localhost: 9000/WS/helloworld? To determine whether the Web Service is correctly started.
Run the client test class and output Hello kongxx on the command line! .