Web Services开发(Java篇)
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。
Web Service的关键技术和规则:
1.XML:描述数据的标准方法.
2.SOAP:表示信息交换的协议(简单对象访问协议).
3.WSDL:Web服务描述语言.
4.UDDI:通用描述、发现与集成,他是一种独立于平台,基于XML语言的用于在互联网上描述商务的协议。
一、利用JDK web服务api实现,这里使用基于SOAP message的Web Service:
1.首先创建一个Web Services项目,作为Web services Endpoint.
2.创建一个HelloService.java类
- package com.yjpeng.hello;
- import javax.jws.WebService;
- import javax.jws.WebMethod;
- import javax.xml.ws.Endpoint;
- @SOAPBinding(style = SOAPBinding.Style.RPC)
- @WebService
- public class HelloService {
- 10.
- 11. @WebMethod
- 12. public String sayHello(String message){
- 13. return "Hello ," + message;
- 14. }
- 15.
- 16. public static void main(String[] args) {
- 17. //create and publish an endPoint
- 18. HelloService hello = new HelloService();
- 19. Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);
- 20. }
21. }
- package com.yjpeng.hello;
- import javax.jws.WebService;
- import javax.jws.WebMethod;
- import javax.xml.ws.Endpoint;
- @WebService
- public class HelloService {
- 10. @WebMethod
- 11. public String sayHello(String message){
- 12. return "Hello ," + message;
- 13. }
- 14.
- 15. public static void main(String[] args) {
- 16. //create and publish an endPoint
- 17. HelloService hello = new HelloService();
- 18. Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);
- 19. }
20. }
3.使用apt编译HelloService.java(例如: apt -d bin(bin存放编译后的文件目录) scr/com/yjpeng/hello/HelloService.java)后,会生成jaxws目录。
4.使用java com.yjpeng.hello.HelloService执行HelloService.java文件,在浏览器中输入http://localhost:8080/helloService?wsdl出现如下图
5.使用wsimport命令生成客户端:wsimport -p com.yjpeng.webservice -keep http://localhost:8080/helloService?wsdl 这时会在当前目录中生成如下文件:
6.编写好客户端文件HelloClient.java
- package com.yjpeng.hello;
- import com.yjpeng.webservice.HelloServiceService;
- public class HelloClient {
- public static void main(String[] args) {
- HelloServiceService helloServiceService = new HelloServiceService();
- com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();
- System.out.println(helloService.sayHello("你好"));
- 10. }
- 11.
12. }
- package com.yjpeng.hello;
- import com.yjpeng.webservice.HelloServiceService;
- public class HelloClient {
- public static void main(String[] args) {
- HelloServiceService helloServiceService = new HelloServiceService();
- com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();
- System.out.println(helloService.sayHello("你好"));
- 10. }
- 11.
12. }
运行结果在控制台输出 hello,你好 证明利用JDK web服务API实现web service成功!
二、使用xfire框架,我这里使用的是MyEclipse集成的xfire进行测试,利用xfire开发WebService可以有三种方法:
a.一种是从JavaBean中生成。
b.一种是从wsdl文件中生成。
c.一种是自己建立webservice。
具体实现步骤如下:
1.用Myeclipse建立webService工程(注意:Web Service&J2EE Details中的Framework选XFire),目录结构如下:
2.创建IHello.java接口
- package com.yjpeng.hello;
- public interface IHello {
- public String sayHello(String message);
- }
- package com.yjpeng.hello;
- public interface IHello {
- public String sayHello(String message);
- }
3.创建IHelloImpl.java实现IHello.java接口
- package com.yjpeng.hello;
- public class IHelloImpl implements IHello {
- public String sayHello(String message) {
- return message;
- }
- }
- package com.yjpeng.hello;
- public class IHelloImpl implements IHello {
- public String sayHello(String message) {
- return message;
- }
- }
4.修改Service.xml文件,加入以下代码
<service>
<!-- 为该Service起一个名字 -->
<name>HelloService</name>
<!-- service的接口类 -->
<serviceClass>
com.yjpeng.hello.IHello
</serviceClass>
<!-- service的接口实现类-->
<implementationClass>
com.yjpeng.hello.IHelloImpl
</implementationClass>
<!-- wsdl的样式-->
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
5.把该Web Service项目部署到tomcat里面启动tomcat在浏览器地址栏输入http://localhost:8080/TestWebServices/services/Hello?wsdl 出现如下图
6.然后在展开HelloService后面的wsdl可以看到
7.创建一个客户端HelloClient.java类
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.codehaus.xfire.XFireFactory;
- import org.codehaus.xfire.client.Client;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
10. import com.yjpeng.hello.IHello;
- 11.
12. public class HelloClient {
- 13. public static void main(String[] args) {
- 14. Service s = new ObjectServiceFactory().create(IHello.class);
- 15. XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
- 16. String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";
- 17. IHello hello;
- 18. try {
- 19. hello = (IHello)xf.create(s, url);
- 20. System.out.println(hello.sayHello("你好"));
- 21. } catch (MalformedURLException e) {
- 22. e.printStackTrace();
- 23. }
- 24. try {
- 25. //这个是在java端调用.net写的远程Web Service 如果调用本机写的只需要把URL中的地址换成本机能访问的地址即可
- 26. Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
- 27. Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});
- 28. System.out.println(o.length);
- 29. } catch (Exception e) {
- 30. e.printStackTrace();
- 31. }
- 32. }
33. }
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.codehaus.xfire.XFireFactory;
- import org.codehaus.xfire.client.Client;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
10. import com.yjpeng.hello.IHello;
- 11.
12. public class HelloClient {
- 13. public static void main(String[] args) {
- 14. Service s = new ObjectServiceFactory().create(IHello.class);
- 15. XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
- 16. String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";
- 17. IHello hello;
- 18. try {
- 19. hello = (IHello)xf.create(s, url);
- 20. System.out.println(hello.sayHello("你好"));
- 21. } catch (MalformedURLException e) {
- 22. e.printStackTrace();
- 23. }
- 24. try {
- 25. //这个是在java端调用.net写的远程Web Service 如果调用本机写的只需要把URL中的地址换成本机能访问的地址即可
- 26. Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
- 27. Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});
- 28. System.out.println(o.length);
- 29. } catch (Exception e) {
- 30. e.printStackTrace();
- 31. }
- 32. }
33. }
运行HelloClient.java类可以输出 你好 1.
三、使用axis1.4开发webservice方法
首先下载axis1.4包和tomcat服务器,并将解压后的axis1.4包下面的webapps下的axis目录复制到tomcat服务器的webapps文件夹中。axis支持三种Web Service的部署和开发,分别为:
a.Dynamic Invocation Interface(DII)
b.Stubs 方式
c.Dynamic Proxy方式
1.编写DII(Dynamic Invocation Interface)方式Web Service
a.编写服务程序HelloClient.java
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
b.将HelloClient.java文件拷贝到axis_home下,重命名为HelloClient.jws.
c.访问链接http://localhost:8080/axis/HelloClient.jws?wsdl页面显示axis自动生成的wsdl文件
d.编写访问服务的客户端TestHelloClient.java需要导入相应的axis.jar包,在下载的axis的WEB-INF/lib/目录下。
- package com.yjpeng.webservice;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class TestHelloClient {
- public static void main(String[] args){
- 10. try{
- 11. String endpoint = "http://localhost:8080/axis/HelloClient.jws";
- 12. Service service = new Service();
- 13. Call call = (Call)service.createCall();
- 14. call.setOperationName(new QName(endpoint, "getName"));
- 15. call.setTargetEndpointAddress(new URL(endpoint));
- 16. String result = (String) call.invoke(new Object[]{"张三"});
- 17. System.out.println(result);
- 18. }catch (Exception e) {
- 19. e.printStackTrace();
- 20. }
- 21. }
22. }
- package com.yjpeng.webservice;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class TestHelloClient {
- public static void main(String[] args){
- 10. try{
- 11. String endpoint = "http://localhost:8080/axis/HelloClient.jws";
- 12. Service service = new Service();
- 13. Call call = (Call)service.createCall();
- 14. call.setOperationName(new QName(endpoint, "getName"));
- 15. call.setTargetEndpointAddress(new URL(endpoint));
- 16. String result = (String) call.invoke(new Object[]{"张三"});
- 17. System.out.println(result);
- 18. }catch (Exception e) {
- 19. e.printStackTrace();
- 20. }
- 21. }
22. }
运行TestHelloClient.java在控制台输出hell,张三,测试成功.
2.编写Dynamci Proxy方式访问服务
a.编写部署服务端程序,用上边DII方式使用的HelloClient.java
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
b.编写代理接口HelloClientInterface.java需要扩展java.rmi.Remote类
- package com.yjpeng.dynamic.proxy;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface HelloClientInterface extends Remote {
- public String getName(String name) throws RemoteException;
- }
- package com.yjpeng.dynamic.proxy;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface HelloClientInterface extends Remote {
- public String getName(String name) throws RemoteException;
- }
c.编写访问服务的客户端TestHelloClient.java
- package com.yjpeng.dynamic.proxy;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.rpc.Service;
- import javax.xml.rpc.ServiceFactory;
- public class TestHelloClient {
- 10. public static void main(String[] args){
- 11. try{
- 12. String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";
- 13. String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";
- 14. String serviceName = "HelloClientService";
- 15. String portName = "HelloClient";
- 16. ServiceFactory serviceFactory = ServiceFactory.newInstance();
- 17. Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));
- 18. HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
- 19. HelloClientInterface.class);
- 20. System.out.println(proxy.getName("张三"));
- 21. }catch (Exception e) {
- 22. e.printStackTrace();
- 23. }
- 24. }
25. }
- package com.yjpeng.dynamic.proxy;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.rpc.Service;
- import javax.xml.rpc.ServiceFactory;
- public class TestHelloClient {
- 10. public static void main(String[] args){
- 11. try{
- 12. String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";
- 13. String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";
- 14. String serviceName = "HelloClientService";
- 15. String portName = "HelloClient";
- 16. ServiceFactory serviceFactory = ServiceFactory.newInstance();
- 17. Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));
- 18. HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
- 19. HelloClientInterface.class);
- 20. System.out.println(proxy.getName("张三"));
- 21. }catch (Exception e) {
- 22. e.printStackTrace();
- 23. }
- 24. }
25. }
运行TestHelloClient.java在控制台输出hell,张三,测试成功.
四、使用axis2开发webservice
研究中....
五、在java web项目中开放一个webservice接口实例
1.引入需要的jar包
2.创建一个接口类IAddNumbers.java
- package com.yjpeng.webservice;
- public interface IAddNumbers {
- public int addNumbers(int a, int b);
- }
- package com.yjpeng.webservice;
- public interface IAddNumbers {
- public int addNumbers(int a, int b);
- }
3.创建一个AddNumberImpl.java实现IAddnumbers.java接口类
- package com.yjpeng.webservice;
- import javax.jws.WebService;
- import com.sun.xml.ws.transport.http.servlet.WSServlet;
- @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",
- portName="AddNumberImpl")
- public class AddNumberImpl implements IAddNumbers {
- 10. public int addNumbers(int a, int b) {
- 11. return a + b;
- 12. }
13. }
- package com.yjpeng.webservice;
- import javax.jws.WebService;
- import com.sun.xml.ws.transport.http.servlet.WSServlet;
- @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",
- portName="AddNumberImpl")
- public class AddNumberImpl implements IAddNumbers {
- 10. public int addNumbers(int a, int b) {
- 11. return a + b;
- 12. }
13. }
4.在WEN-INF目录下创建一个sun-jaxws.xml文件
- <?xml version="1.0"?>
- <endpoints version="2.0"
- xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
- <endpoint name="AddNumberImpl"
- implementation="com.yjpeng.webservice.AddNumberImpl"
- url-pattern="/addNumberImpl"/>
- </endpoints>
- <?xml version="1.0"?>
- <endpoints version="2.0"
- xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
- <endpoint name="AddNumberImpl"
- implementation="com.yjpeng.webservice.AddNumberImpl"
- url-pattern="/addNumberImpl"/>
- </endpoints>
5.在web.xml文件中增加
- <servlet>
- <servlet-name>AddNumberService</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>AddNumberService</servlet-name>
- <url-pattern>/addNumberImpl</url-pattern>
- </servlet-mapping>
10. <listener>
- 11. <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
12. </listener>
- <servlet>
- <servlet-name>AddNumberService</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>AddNumberService</servlet-name>
- <url-pattern>/addNumberImpl</url-pattern>
- </servlet-mapping>
10. <listener>
- 11. <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
12. </listener>
6.部署web项目 启动相应部署服务器。访问http://localhost:8080/TestJaxWebService/addNumberImpl?wsdl 这样每次启动web服务器就开放了一个web service的接口。
相关:
cxf:源码:https://github.com/apache/cxf
利用AXIS开发Webservice(一) —— 如何发布自己的webservice
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。
Web Service的关键技术和规则:
1.XML:描述数据的标准方法.
2.SOAP:表示信息交换的协议(简单对象访问协议).
3.WSDL:Web服务描述语言.
4.UDDI:通用描述、发现与集成,他是一种独立于平台,基于XML语言的用于在互联网上描述商务的协议。
一、利用JDK web服务api实现,这里使用基于SOAP message的Web Service:
1.首先创建一个Web Services项目,作为Web services Endpoint.
2.创建一个HelloService.java类
- package com.yjpeng.hello;
- import javax.jws.WebService;
- import javax.jws.WebMethod;
- import javax.xml.ws.Endpoint;
- @WebService
- public class HelloService {
- 10. @WebMethod
- 11. public String sayHello(String message){
- 12. return "Hello ," + message;
- 13. }
- 14.
- 15. public static void main(String[] args) {
- 16. //create and publish an endPoint
- 17. HelloService hello = new HelloService();
- 18. Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);
- 19. }
20. }
- package com.yjpeng.hello;
- import javax.jws.WebService;
- import javax.jws.WebMethod;
- import javax.xml.ws.Endpoint;
- @WebService
- public class HelloService {
- 10. @WebMethod
- 11. public String sayHello(String message){
- 12. return "Hello ," + message;
- 13. }
- 14.
- 15. public static void main(String[] args) {
- 16. //create and publish an endPoint
- 17. HelloService hello = new HelloService();
- 18. Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);
- 19. }
20. }
3.使用apt编译HelloService.java(例如: apt -d bin(bin存放编译后的文件目录) scr/com/yjpeng/hello/HelloService.java)后,会生成jaxws目录。
4.使用java com.yjpeng.hello.HelloService执行HelloService.java文件,在浏览器中输入http://localhost:8080/helloService?wsdl出现如下图
5.使用wsimport命令生成客户端:wsimport -p com.yjpeng.webservice -keep http://localhost:8080/helloService?wsdl 这时会在当前目录中生成如下文件:
6.编写好客户端文件HelloClient.java
- package com.yjpeng.hello;
- import com.yjpeng.webservice.HelloServiceService;
- public class HelloClient {
- public static void main(String[] args) {
- HelloServiceService helloServiceService = new HelloServiceService();
- com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();
- System.out.println(helloService.sayHello("你好"));
- 10. }
- 11.
12. }
- package com.yjpeng.hello;
- import com.yjpeng.webservice.HelloServiceService;
- public class HelloClient {
- public static void main(String[] args) {
- HelloServiceService helloServiceService = new HelloServiceService();
- com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();
- System.out.println(helloService.sayHello("你好"));
- 10. }
- 11.
12. }
运行结果在控制台输出 hello,你好 证明利用JDK web服务API实现web service成功!
二、使用xfire框架,我这里使用的是MyEclipse集成的xfire进行测试,利用xfire开发WebService可以有三种方法:
a.一种是从JavaBean中生成。
b.一种是从wsdl文件中生成。
c.一种是自己建立webservice。
具体实现步骤如下:
1.用Myeclipse建立webService工程(注意:Web Service&J2EE Details中的Framework选XFire),目录结构如下:
2.创建IHello.java接口
- package com.yjpeng.hello;
- public interface IHello {
- public String sayHello(String message);
- }
- package com.yjpeng.hello;
- public interface IHello {
- public String sayHello(String message);
- }
3.创建IHelloImpl.java实现IHello.java接口
- package com.yjpeng.hello;
- public class IHelloImpl implements IHello {
- public String sayHello(String message) {
- return message;
- }
- }
- package com.yjpeng.hello;
- public class IHelloImpl implements IHello {
- public String sayHello(String message) {
- return message;
- }
- }
4.修改Service.xml文件,加入以下代码
<service>
<!-- 为该Service起一个名字 -->
<name>HelloService</name>
<!-- service的接口类 -->
<serviceClass>
com.yjpeng.hello.IHello
</serviceClass>
<!-- service的接口实现类-->
<implementationClass>
com.yjpeng.hello.IHelloImpl
</implementationClass>
<!-- wsdl的样式-->
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
5.把该Web Service项目部署到tomcat里面启动tomcat在浏览器地址栏输入http://localhost:8080/TestWebServices/services/Hello?wsdl 出现如下图
6.然后在展开HelloService后面的wsdl可以看到
7.创建一个客户端HelloClient.java类
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.codehaus.xfire.XFireFactory;
- import org.codehaus.xfire.client.Client;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
10. import com.yjpeng.hello.IHello;
- 11.
12. public class HelloClient {
- 13. public static void main(String[] args) {
- 14. Service s = new ObjectServiceFactory().create(IHello.class);
- 15. XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
- 16. String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";
- 17. IHello hello;
- 18. try {
- 19. hello = (IHello)xf.create(s, url);
- 20. System.out.println(hello.sayHello("你好"));
- 21. } catch (MalformedURLException e) {
- 22. e.printStackTrace();
- 23. }
- 24. try {
- 25. //这个是在java端调用.net写的远程Web Service 如果调用本机写的只需要把URL中的地址换成本机能访问的地址即可
- 26. Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
- 27. Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});
- 28. System.out.println(o.length);
- 29. } catch (Exception e) {
- 30. e.printStackTrace();
- 31. }
- 32. }
33. }
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.codehaus.xfire.XFireFactory;
- import org.codehaus.xfire.client.Client;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
10. import com.yjpeng.hello.IHello;
- 11.
12. public class HelloClient {
- 13. public static void main(String[] args) {
- 14. Service s = new ObjectServiceFactory().create(IHello.class);
- 15. XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
- 16. String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";
- 17. IHello hello;
- 18. try {
- 19. hello = (IHello)xf.create(s, url);
- 20. System.out.println(hello.sayHello("你好"));
- 21. } catch (MalformedURLException e) {
- 22. e.printStackTrace();
- 23. }
- 24. try {
- 25. //这个是在java端调用.net写的远程Web Service 如果调用本机写的只需要把URL中的地址换成本机能访问的地址即可
- 26. Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
- 27. Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});
- 28. System.out.println(o.length);
- 29. } catch (Exception e) {
- 30. e.printStackTrace();
- 31. }
- 32. }
33. }
运行HelloClient.java类可以输出 你好 1.
三、使用axis1.4开发webservice方法
首先下载axis1.4包和tomcat服务器,并将解压后的axis1.4包下面的webapps下的axis目录复制到tomcat服务器的webapps文件夹中。axis支持三种Web Service的部署和开发,分别为:
a.Dynamic Invocation Interface(DII)
b.Stubs 方式
c.Dynamic Proxy方式
1.编写DII(Dynamic Invocation Interface)方式Web Service
a.编写服务程序HelloClient.java
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
b.将HelloClient.java文件拷贝到axis_home下,重命名为HelloClient.jws.
c.访问链接http://localhost:8080/axis/HelloClient.jws?wsdl页面显示axis自动生成的wsdl文件
d.编写访问服务的客户端TestHelloClient.java需要导入相应的axis.jar包,在下载的axis的WEB-INF/lib/目录下。
- package com.yjpeng.webservice;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class TestHelloClient {
- public static void main(String[] args){
- 10. try{
- 11. String endpoint = "http://localhost:8080/axis/HelloClient.jws";
- 12. Service service = new Service();
- 13. Call call = (Call)service.createCall();
- 14. call.setOperationName(new QName(endpoint, "getName"));
- 15. call.setTargetEndpointAddress(new URL(endpoint));
- 16. String result = (String) call.invoke(new Object[]{"张三"});
- 17. System.out.println(result);
- 18. }catch (Exception e) {
- 19. e.printStackTrace();
- 20. }
- 21. }
22. }
- package com.yjpeng.webservice;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class TestHelloClient {
- public static void main(String[] args){
- 10. try{
- 11. String endpoint = "http://localhost:8080/axis/HelloClient.jws";
- 12. Service service = new Service();
- 13. Call call = (Call)service.createCall();
- 14. call.setOperationName(new QName(endpoint, "getName"));
- 15. call.setTargetEndpointAddress(new URL(endpoint));
- 16. String result = (String) call.invoke(new Object[]{"张三"});
- 17. System.out.println(result);
- 18. }catch (Exception e) {
- 19. e.printStackTrace();
- 20. }
- 21. }
22. }
运行TestHelloClient.java在控制台输出hell,张三,测试成功.
2.编写Dynamci Proxy方式访问服务
a.编写部署服务端程序,用上边DII方式使用的HelloClient.java
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
- public class HelloClient
- {
- public String getName(String name){
- return "hello," + name;
- }
- }
b.编写代理接口HelloClientInterface.java需要扩展java.rmi.Remote类
- package com.yjpeng.dynamic.proxy;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface HelloClientInterface extends Remote {
- public String getName(String name) throws RemoteException;
- }
- package com.yjpeng.dynamic.proxy;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface HelloClientInterface extends Remote {
- public String getName(String name) throws RemoteException;
- }
c.编写访问服务的客户端TestHelloClient.java
- package com.yjpeng.dynamic.proxy;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.rpc.Service;
- import javax.xml.rpc.ServiceFactory;
- public class TestHelloClient {
- 10. public static void main(String[] args){
- 11. try{
- 12. String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";
- 13. String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";
- 14. String serviceName = "HelloClientService";
- 15. String portName = "HelloClient";
- 16. ServiceFactory serviceFactory = ServiceFactory.newInstance();
- 17. Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));
- 18. HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
- 19. HelloClientInterface.class);
- 20. System.out.println(proxy.getName("张三"));
- 21. }catch (Exception e) {
- 22. e.printStackTrace();
- 23. }
- 24. }
25. }
- package com.yjpeng.dynamic.proxy;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.rpc.Service;
- import javax.xml.rpc.ServiceFactory;
- public class TestHelloClient {
- 10. public static void main(String[] args){
- 11. try{
- 12. String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";
- 13. String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";
- 14. String serviceName = "HelloClientService";
- 15. String portName = "HelloClient";
- 16. ServiceFactory serviceFactory = ServiceFactory.newInstance();
- 17. Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));
- 18. HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
- 19. HelloClientInterface.class);
- 20. System.out.println(proxy.getName("张三"));
- 21. }catch (Exception e) {
- 22. e.printStackTrace();
- 23. }
- 24. }
25. }
运行TestHelloClient.java在控制台输出hell,张三,测试成功.
四、使用axis2开发webservice
研究中....
五、在java web项目中开放一个webservice接口实例
1.引入需要的jar包
2.创建一个接口类IAddNumbers.java
- package com.yjpeng.webservice;
- public interface IAddNumbers {
- public int addNumbers(int a, int b);
- }
- package com.yjpeng.webservice;
- public interface IAddNumbers {
- public int addNumbers(int a, int b);
- }
3.创建一个AddNumberImpl.java实现IAddnumbers.java接口类
- package com.yjpeng.webservice;
- import javax.jws.WebService;
- import com.sun.xml.ws.transport.http.servlet.WSServlet;
- @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",
- portName="AddNumberImpl")
- public class AddNumberImpl implements IAddNumbers {
- 10. public int addNumbers(int a, int b) {
- 11. return a + b;
- 12. }
13. }
- package com.yjpeng.webservice;
- import javax.jws.WebService;
- import com.sun.xml.ws.transport.http.servlet.WSServlet;
- @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",
- portName="AddNumberImpl")
- public class AddNumberImpl implements IAddNumbers {
- 10. public int addNumbers(int a, int b) {
- 11. return a + b;
- 12. }
13. }
4.在WEN-INF目录下创建一个sun-jaxws.xml文件
- <?xml version="1.0"?>
- <endpoints version="2.0"
- xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
- <endpoint name="AddNumberImpl"
- implementation="com.yjpeng.webservice.AddNumberImpl"
- url-pattern="/addNumberImpl"/>
- </endpoints>
- <?xml version="1.0"?>
- <endpoints version="2.0"
- xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
- <endpoint name="AddNumberImpl"
- implementation="com.yjpeng.webservice.AddNumberImpl"
- url-pattern="/addNumberImpl"/>
- </endpoints>
5.在web.xml文件中增加
- <servlet>
- <servlet-name>AddNumberService</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>AddNumberService</servlet-name>
- <url-pattern>/addNumberImpl</url-pattern>
- </servlet-mapping>
10. <listener>
- 11. <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
12. </listener>
- <servlet>
- <servlet-name>AddNumberService</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>AddNumberService</servlet-name>
- <url-pattern>/addNumberImpl</url-pattern>
- </servlet-mapping>
10. <listener>
- 11. <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
12. </listener>
需要源代码可以私信