CXF入門教程(1) -- 第一個webService

不淨之心發表於2013-04-06
參考:[url]http://blog.csdn.net/neareast/article/details/7714778[/url]
本程式碼使用Maven來管理依賴
[b]pom.xml[/b]
<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.pandy</groupId>
<artifactId>webservice</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webservice Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>3.2.1.RELEASE</spring.version>
<tomcat.version>2.1-SNAPSHOT</tomcat.version>
<junit.version>3.8.1</junit.version>
</properties>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>


<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.7.3</version>
</dependency>

</dependencies>
<build>
<finalName>webservice</finalName>
</build>
</project>


[b]javaBean[/b]
package com.webservices.bean;

public class User {

private String name;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "User [userName=" + name + ", email=" + email + "]";
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}


[b]介面[/b]:首先我們寫一個serivce介面,其中包含一個操作sayHi,它的作用是向其提交名字的人問好。
package com.webservices;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.webservices.bean.User;
/**
* 將要用來發布的介面
* @author pandy
*
*/
@SuppressWarnings("restriction")
@WebService
public interface iHelloWorld {

// 加入WebParam註解,以保證xml檔案中引數名字的正確性
String sayHi(@WebParam(name = "text") String text);
String sayHiToUser(User user);

}


[b]實現類[/b]: WebParam註解是必須的,因為java藉口編譯後的.class檔案不儲存引數的名字,所以如果沒有加註解,引數將被命名為arg0。介面實現部分的示例如下:
package com.webservices.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.jws.WebService;

import com.webservices.iHelloWorld;
import com.webservices.bean.User;
/**
* 介面的實現類
* @author pandy
*
*/
@SuppressWarnings({ "restriction" })
@WebService(endpointInterface = "com.webservices.iHelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements iHelloWorld {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();

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

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

}

@WebService註解讓CXF知道我們希望使用哪個介面來建立WSDL,本例中就是iHelloWorld介面。

[b]服務釋出類[/b]
import javax.xml.ws.Endpoint;

import com.webservices.impl.HelloWorldImpl;

/**
* 釋出程式
* @author pandy
*
*/
@SuppressWarnings("restriction")
public class ServiceTest {

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

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

Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}

}


進入: [url]http://localhost:8080/helloWorld?wsdl[/url]
This XML file does not appear to have any style information associated
with it. The document tree is shown below.
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
JAX-WS RI 2.2.4-b01. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is
JAX-WS RI 2.2.4-b01. -->
<definitions
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://impl.webservices.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.webservices.com/"
name="HelloWorld">
<import namespace="http://webservices.com/" location="http://localhost:8080/helloWorld?wsdl=1" />
<binding xmlns:ns1="http://webservices.com/" name="HelloWorldImplPortBinding"
type="ns1:iHelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<operation name="sayHi">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="sayHiToUser">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="HelloWorld">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:8080/helloWorld" />
</port>
</service>
</definitions>

相關文章