JDK6中的WebService运用
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。
Web Service的关键技术和规则:
1.XML:描述数据的标准方法.
2.SOAP:表示信息交换的协议(简单对象访问协议).
3.WSDL:Web服务描述语言.
4.UDDI:通用描述、发现与集成,他是一种独立于平台,基于XML语言的用于在互联网上描述商务的协议。
一、利用JDK web服务api实现,这里使用基于SOAP message的Web Service:
1.首先创建一个WS.Server项目,作为Web services Endpoint.
2.创建一个HelloService.java类
package com.demo.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class HelloService {
@WebMethod
public String sayHello(String world) {
return "Hello " + world;
}
public static void main(String[] args) {
HelloService hello = new HelloService();
Endpoint point = Endpoint.publish("http://localhost/helloService", hello);
System.out.println("服务器端正在运行 ......");
}
}
3.使用apt编译HelloService.java
D:/test/WS.Server>apt –d bin src/com/demo/service/HelloService.java
或写build.xml
<project name="ws.server" default="apt" basedir=".">
<target name="apt">
<exec dir="${basedir}" executable="apt">
<arg line="-d bin src/com/demo/service/HelloService.java"/>
</exec>
</target>
</project>
将会在D:/test/WS.Server/bin/com/demo/service/jaxws目录下生成SayHello.class和SayHelloResponse.class文件。
4.运行HelloService.java
D:/test/WS.Server/src>java com.demo.service.HelloService.java
在浏览器中输入http://localhost/helloService?wsdl
将会出现:
5. 创建一个WS.Client项目,作为Web services 客户端.
6.使用wsimport命令生成客户端程序
D:/test/WS.Client/src>wsimport –p com.demo.client –keep http://localhost/helloService?wsdl
或写build.xml文件
<project name="ws.client" default="wsimport" basedir=".">
<target name="clean">
<delete verbose="true">
<fileset dir="${basedir}/src/com/demo/client" includes="**/*.class"/>
</delete>
</target>
<target name="wsimport" depends="clean">
<exec dir="${basedir}/src" executable="wsimport">
<arg line="-p com.demo.client -keep http://localhost/helloService?wsdl" />
</exec>
</target>
</project>
将会在D:/test/WS.Client/src/com/demo/client下生成客户端java文件。
7.编写客户端测试类
package com.demo.test;
import com.demo.client.HelloService;
import com.demo.client.HelloServiceService;
public class WSTest {
public static void main(String[] args) {
HelloServiceService service = new HelloServiceService();
HelloService hello = service.getHelloServicePort();
System.out.println(hello.sayHello("China"));
}
}
包结构:
二、Deploy JAX-WS web services on Tomcat
1.首先创建一个WS.Web项目,作为Web services Endpoint.
2.创建一个HelloService接口
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloService {
@WebMethod
public String sayHello(String world);
}
3.创建一个实现类
@WebService(endpointInterface = "com.demo.ws.HelloService")
public class HelloServiceImpl implements HelloService {
@Override
@WebMethod
public String sayHello(String world) {
return "Hello " + world;
}
}
3.下载JAX-WS所依赖的jar
添加到工程的类路径
4.编写sun-jaxws.xml放在WebContent/WEB-INF/目录下
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="HelloWorld" implementation="com.demo.ws.HelloServiceImpl" url-pattern="/hello" />
</endpoints>
5.在web.xml中添加
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
6.部署到Tomcat中,并启动Tomcat
7.在浏览器中输入http://localhost/WS.Web/hello
将出现:
8.创建一个WS.Client工程作为客户端
9. 使用wsimport命令生成客户端程序
D:/test/WS.Client/src>wsimport –p com.demo.client –keep http://localhost:80/WS.Web/hello?wsdl
或写build.xml文件
<project name="ws.client" default="wsimport" basedir=".">
<target name="clean">
<delete verbose="true">
<fileset dir="${basedir}/src/com/demo/client" includes="**/*.class"/>
</delete>
</target>
<target name="wsimport" depends="clean">
<exec dir="${basedir}/src" executable="wsimport">
<arg line="-p com.demo.client -keep http://localhost:80/WS.Web/hello?wsdl" />
</exec>
</target>
</project>
将会在D:/test/WS.Client/src/com/demo/client下生成客户端java文件。
10.写一个测试类
public class WSTest {
public static void main(String[] args) {
HelloServiceImplService service = new HelloServiceImplService();
HelloService hello = service.getHelloServiceImplPort();
System.out.println(hello.sayHello("China"));
}
}