WebService
- 简单介绍
Webservice是多个应用程序之间信息交流的媒介。
Webservice描述三个角色:服务提供者,服务请求者和服务中介者。
Webservice的三个操作:发布、查找和绑定
服务提供者通过服务中介处注册并发布服务,服务请求者通过服务中介发现服务并定位到服务。服务请求者绑定服务提供者并使用特定的绑定。
- webservice发布
a.在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.4.0.RELEASE</version>
</dependency>
b.webservice接口及实现类
@WebService
public interface IService {
@WebMethod
public String HelloWorld(String userName);
}
实现类:Webservice中serviceName=“实现类名” endpointInterface=“接口名”。
@Component
@WebService(serviceName = "UserService", endpointInterface = "com.soft.service.IService")
public class UserService implements IService {
public String HelloWorld(String userName) {
return userName+" HelloWord";
}
}
c.在web.xml加入监听和context配置文件
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
context-param配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
beans.xml配置:
<context:annotation-config/> <!--注解支持-->
<context:component-scan base-package="com.soft"/> <!--注解扫描-->
<!--发布服务 baseAddress:服务地址-->
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8089/services/"/>
</bean>
d.在地址栏输入服务地址http://localhost:8089/services/UserService?wsdl

- 调用Webservice
A.创建客户端程序

B.在cmd中切换到/src/main/java目录下,输入 wsimport -s http://localhost:8089/services/UserService?wsdl生成代码。
C.在/src/main/java目录下,创建测试类。
public class Test {
public static void main(String[] args){
IService is=new UserService().getUserServicePort();
System.out.println(is.helloWorld("caesar"));
}
}
测试结果见下:

浙公网安备 33010602011771号