参考:
http://blog.csdn.net/neareast/article/details/7714778
本代码使用Maven来管理依赖
pom.xml
<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>
javaBean
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;
}
}
接口:首先我们写一个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);
}
实现类: 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接口。
服务发布类
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);
}
}
进入:
http://localhost:8080/helloWorld?wsdl
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>