参考:
http://tsinglongwu.iteye.com/blog/832704
测试调试工具介绍
1.SoapUI1.6
http://webservices.ctocio.com.cn/tips/263/7817763.shtml
2.TestMaker
http://webservices.ctocio.com.cn/tips/263/7817763_2.shtml
3.WebServiceStudio
http://demo.tc/Post/529
SoapUI支持eclipse插件
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>
<!-- Spring jars -->
<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>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- CXF jars -->
<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>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>webservice</finalName>
</build>
</project>
提供服务端
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
非注解:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 心版本去掉cxf/cxf-extension-soap.xml-->
<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->
<bean id="hello" class="com.webservices.impl.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello"
address="/HelloWorld" />
</beans>
<jaxws:endpoint>有三个属性id, implementor和address。
“
id”指定这个Bean在Spring上下文中唯一的标识。
“
implementor”指定了这个Web Service的实现类。
“
address”指定了服务在Web服务器上发布的地址。这个地址可以包含ip和端口的完整地址,也可以是只包含相对路径的地址。
注解:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<context:annotation-config />
<context:component-scan base-package="com" />
<jaxws:endpoint id="helloWorld" implementor="#helloWorldImpl"
address="/HelloWorld" />
</beans>
注意: 同时应该在实现类加上@Component注解.
接口:使用@WebService注解,表明这个接口就是对外提供的服务.
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);
}
实现类:使用@WebService(endpointInterface = "com.webservices.IHelloWorld", serviceName = "HelloWorld")注解,表明这个是IHelloWorld服务的实现类,并提供发布到外部,服务名字是HelloWorld
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();
}
}
也可以使用@Component去注解实现类,才能使用注解方式.
运行工程,进入:
http://localhost:8080/webservice/HelloWorld?wsdl ,得到的wsdl
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.webservices.com/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http"
xmlns:ns1="http://webservices.com/" name="HelloWorld" targetNamespace="http://impl.webservices.com/">
<wsdl:import
location="http://localhost:8080/webservice/HelloWorld?wsdl=IHelloWorld.wsdl"
namespace="http://webservices.com/"></wsdl:import>
<wsdl:binding name="HelloWorldSoapBinding" type="ns1:IHelloWorld">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document" />
<wsdl:input name="sayHi">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHiResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHiToUser">
<soap:operation soapAction="" style="document" />
<wsdl:input name="sayHiToUser">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHiToUserResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorld">
<wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/webservice/HelloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
则表示服务端的webservice已经部署成功,就等着客户端来访问.
客户端:
pom.xml,
类似服务端的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>webserviceclient</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webserviceclient 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>
......
</dependencies>
<build>
<finalName>webserviceclient</finalName>
</build>
</project>
使用命令得到基本的类:
wsdl2java -d /mnt/d/vmware_shared_folder/workspace/webserviceclient/src/main/java -p com.websercices.client -frontend jaxws21 http://localhost:8080/webservice/HelloWorld?wsdl
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="com.websercices.client.IHelloWorld" factory-bean="clientFactory"
factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.websercices.client.IHelloWorld" />
<property name="address" value="http://localhost:8080/webservice/HelloWorld" />
</bean>
</beans>
注意:
1. org.apache.cxf.jaxws.JaxWsProxyFactoryBean要知道提供服务的接口和URL.
2. <property name="address" value="http://localhost:8080/webservice/HelloWorld" />中的/HelloWorld要与服务器端applicationContext.xml中的<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address属性对应。
建立一个测试类:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.websercices.client.IHelloWorld;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
IHelloWorld server = (IHelloWorld) ctx.getBean("client");
String result = server.sayHi("猪头!");
System.out.println(result);
}
}
运行客户端测试类,得到结果: Hello 猪头!