WebService WSDL Java example

http://blog.csdn.net/yaerfeng/article/details/8352307

Example of WEBSERVICE quick start:

First define the interface:

< div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border- left-width:3px; border-left-style:solid; border-left-color:rgb(153,153,153)"> [java] view plain copy< /span>

print ?

  1. packagecom.whaty.platform.ws.server;
  2. importjavax.jws.WebService;

  3. /**
  4. * @className:IMyservice.java
  5. *@Desc:Definition: SEI service endpoint interface
  6. * @author:lizhuang
  7. * @createTime:2012-12-21 12:57:18 AM
  8. */
  9. // JAX-WS comment, which means java api xml for webservice. JDK comes with API in XML format webservice
  10. < li style="margin:0px!important; padding:0px 3px 0px 10px!important; border-style:none none none solid; border-left-width:3px; border-left-color:rgb(153,153,153); list-style :decimal-leading-zero outside; color:rgb(255,153,153); line-height:18px; background-color:rgb(85,85,85)"> @WebService

  11. < span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(102,204,255); font-weight:bold; background-color:inherit">publicinterface IMyservice{
  12. intadd(inta,intb);
  13. intminus(< /span>int a,int B);
  14. }


Next, write the implementation class:

[java] view plain copy

print?

  1. packagecom.whaty.platform.ws.server;
  2. importjavax.jws.WebService; li>
  3. /**
  4. * @className:MyServiceImpl.java
  5. * @Desc: Definition: SIB service implementation bean
  6. * @author:lizhuang
  7. * @createTime:2012-12-21 01:01:22 AM< /li>
  8. */
[java] view plain copy

print?

  1. //endpointInterface specifies the access point interface: the interface must exist
  2. @WebService(endpointInterface=“com.whaty.platform.ws.server.IMyservice”)
  3. publicclass MyServiceImplimplementsIMyservice{
  4. publicint add(inta, intb){
  5. System.out.println(“a+b=”+(a+b));
  6. return a+b;
  7. publicintminus(inta,intb){
  8. System.out.println(“ab=”+(ab));
  9. returnab;
  10.     }  
  11.   
  12. }  


最后发布我们的服务,直接右键运行main方法,如果控制台没报错,多半是发布成功了,否则检查你的代码:

[java]  view plain  copy

 print ?

  1. package com.whaty.platform.ws.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. /** 
  6.  * @className:MyServer.java 
  7.  * @Desc:发布服务 
  8.  * @author:lizhuang 
  9.  * @createTime:2012-12-21 上午01:02:39 
  10.  */  
  11. public class MyServer {  
  12.     public static  void main(String[] args) {  
  13.         //访问方式:http://localhost:7777/tudou?wsdl  
  14.         String address=“http://localhost:7777/tudou”;  
  15.         Endpoint.publish(address, new MyServiceImpl());  
  16.     }  
  17. }  

浏览器地址栏输入:访问webservice看看是否发布成功【地址后面加上”?wsdl”】:

http://localhost:7777/tudou?wsdl

浏览器显示如下:

[html]  view plain  copy

 print ?

  1. This XML file does not appear to have any style information associated with it. The document tree is shown below.  
  2.   
  3.   
  4. <definitions xmlns:soap=“http://schemas.xmlsoap.org/wsdl/soap/” xmlns:tns=“http://server.ws.platform.whaty.com/” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns=“http://schemas.xmlsoap.org/wsdl/” targetNamespace=“http://server.ws.platform.whaty.com/” name=“MyServiceImplService”>  
  5. <types>  
  6. <xsd:schema>  
  7. <xsd:import namespace=“http://server.ws.platform.whaty.com/” schemaLocation=“http://localhost:7777/tudou?xsd=1”/>  
  8. xsd:schema>  
  9. types>  
  10. <message name=“minus”>  
  11. <part name=“parameters” element=“tns:minus”/>  
  12. message>  
  13. <message name=“minusResponse”>  < /span>
  14. <part name=“parameters” element=“tns:minusResponse”/>  
  15. message>  
  16. <message name=“add”>  
  17. <part name=“parameters” element=“tns:add”/>  
  18. message>  
  19. <message name=“addResponse”>  
  20. <part name=“parameters” element=“tns:addResponse”/>  
  21. message>  
  22. <portType name=“IMyservice”>  
  23. <operation name=“minus”>  
  24. <input message=“tns:minus”/>  
  25. <output message=“tns:minusResponse”/>  
  26. operation>  
  27. <operation name=“add”>  
  28. <input message=“tns:add”/>  
  29. <output message=“tns:addResponse”/>  
  30. operation>  
  31. portType>  
  32. <binding name=“MyServiceImplPort Binding” type=“tns:IMyservice”>  
  33. <soap:binding transport=“http://schemas.xmlsoap.org/soap/http” style=“document”/>  
  34. <operation name=“minus”>  
  35. <soap:operation soapAction=“”/>  
  36. <input>  
  37. <soap:body use=“literal”/>  
  38. input>  
  39. <output>  
  40. <soap:body use=“literal”/>  
  41. output>  
  42. operation>  
  43. <operation name=“add”>  
  44. <soap:operation soapAction=“”/>  
  45. <input>  
  46. <soap:body use=“literal”/>  
  47. input>  
  48. <output>  
  49. <soap:body use=“literal”/>  
  50. output>  
  51. operation>  
  52. bin ding>  
  53. <service name=“MyServiceImplService”>  
  54. <port name=“MyServiceImplPort” binding=< span class="attribute-value" style="margin:0px; padding:0px; border:none; color:rgb(255,255,255); background-color:inherit">“tns:MyServiceImplPortBinding”>  
  55. <soap:address location=“http://localhost:7777/tudou”/>  
  56. port>  
  57. service>  
  58. definitions>  


下面我们创建客户端访问:

[java]  view plain  copy

 print ?

  1. package com.whaty.platform.ws.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5.   
  6. import javax.xml.namespace.QName;  
  7. import javax.xml.ws.Service;  
  8.   
  9. < span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(102,204,255); font-weight:bold; background-color:inherit">import com.whaty.platform.ws.server.IMyservice;  
  10.   
  11. /** 
  12.  * @className:MyClient.java 
  13.  * @Desc:访问发布的服务 
  14.  * @author:lizhuang 
  15.  * @createTime:2012-12-21 上午01:23:57 
  16.  */  
  17. public class MyClient {  
  18.     public static void main(String[] args) {  
  19.   
  20.         try {  
  21.             //服务WSDL Document的地址  
  22.             URL url = new URL(“http://localhost:7777/tudou?wsdl”);  
  23.             //Qnameqname是qualified name 的简写  
  24.             //2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成  
  25.             //由发布的wsdl可知namespace为http://server.ws.platform.whaty.com/,  
  26.             QName qname=new QName(“http://server.ws.platform.whaty.com/”,“MyServiceImplService”);  
  27.             Service service=Service.create(url, qname);  
  28.             IMyservice ms=service.getPort(IMyservice.class);  
  29.             ms.add(14);  
  30.             ms.minus(14);  
  31.         } catch (MalformedURLException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35. }  



控制台打印如下:

a+b=5 a-b=-3

[java]  view plain  copy

 print ?

  1. package com.whaty.platform.ws.server;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /** 
  6.  * @className:IMyservice.java 
  7.  * @Desc:定义:SEI service endpoint interface 
  8.  * @author:lizhuang 
  9.  * @createTime:2012-12-21 上午12:57:18 
  10.  */  
  11. //JAX-WS注解,表示java api xml for webservice。 JDK自带API的XML格式的webservice  
  12. @WebService  
  13. public interface IMyservice {  
  14.       
  15.     int add(int a, int b);  
  16.   
  17.     int minus(int a, int b);  
  18. }  

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

  1. package com.whaty.platform.ws.server;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /**  
  6.  * @className:MyServiceImpl.java 
  7.  * @Desc:定义:SIB service implemention bean 
  8.  * @author:lizhuang 
  9.  * @createTime:2012-12-21 上午01:01:22 
  10.  */  

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

  1. //endpointInterface指定接入点接口:接口必须存在  
  2. @WebService(endpointInterface=“com.whaty.platform.ws.server.IMyservice”)  
  3. public class MyServiceImpl implements IMyservice {  
  4.   
  5.     public int add(int a, int b) {  
  6.         System.out.println(“a+b=”+(a+b));  
  7.         return a+b;  
  8.     }  
  9.   
  10.     public int minus(int a, int b) {  
  11.         System.out.println(“a-b=”+(a-b));  
  12.         return a-b;  
  13.     }  
  14.   
  15. }  

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

  1. package com.whaty.platform.ws.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. /** 
  6.  * @className:MyServer.java 
  7.  * @Desc:发布服务 
  8.  * @author:lizhuang 
  9.  * @createTime:2012-12-21 上午01:02:39 
  10.  */  
  11. public class MyServer {  
  12.     public static void main(String[] args) {  
  13.         //访问方式:http://localhost:7777/tudou?wsdl  
  14.         String address=“http://localhost:7777/tudou”;  
  15.         Endpoint.publish(address, new MyServiceImpl());  
  16.     }  
  17. }  

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

[html]  view plain  copy

 print ?

  1. This XML file does not appear to have any style information associated with it. The document tree is shown below.  
  2.   
  3.   
  4. <definitions xmlns:soap=“http://schemas.xmlsoap.org/wsdl/soap/” xmlns:tns=“http://server.ws.platform.whaty.com/” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns=“http://schemas.xmlsoap.org/wsdl/” targetNamespace=“http://server.ws.platform.whaty.com/” name=“MyServiceImplService”>  
  5. <types>  
  6. <xsd:schema>  
  7. <xsd:import namespace=“http://server.ws.platform.whaty.com/” schemaLocation=“http://localhost:7777/tudou?xsd=1”/>  < /span>
  8. xsd:schema>  
  9. types>  
  10. <message name=“minus”>  
  11. <part name=“parameters” element=“tns:minus”/>  
  12. message>  
  13. <message name=“minusResponse”>  
  14. <part name=“parameters” element=“tns:minusResponse”/>  
  15. message>  
  16. <message name=“add”>  
  17. <part name=“parameters” element=“tns:add”/>  
  18. message>  
  19. <message name=“addResponse “>  
  20. <part name=“parameters” element=“tns:addResponse”/>  
  21. message>  
  22. <portType name=“IMyservice”>  
  23. <operation name=“minus”>  
  24. <input message=“tns:minus”/>  
  25. <output message=“tns:minusResponse”/>  
  26. operation>  
  27. <operation name=“add”>  
  28. <input message=“tns:add”/>  
  29. <output message=“tns:addResponse” />  
  30. operation>  
  31. portType>  
  32. <binding name=“MyServiceImplPortBinding” type=“tns:IMyservice”>  
  33. <soap:binding transport=“http://schemas.xmlsoap.org/soap/http”  style=“document”/>  
  34. <operation name=“minus”>  
  35. <soap:operation soapAction=“”/>  
  36. <input>  
  37. <soap:body use=“literal”/>  
  38. input>  
  39. <output>  
  40. <soap:body use=“literal”/>  
  41. output>  
  42. operation>  
  43. <operation name=“add”>  
  44. <soap:operation soapAction=“”/>  
  45. <input>  
  46. <soap:body use=“literal”/>  
  47. input>  
  48. <output>  
  49. <soap:body use=“literal”/>  
  50. output>  
  51. operation>  
  52. < /binding>  
  53. <service name=“MyServiceImplService”>  
  54. <port name=“MyServiceImplPort” binding=“tns:MyServiceImplPortBinding”>  
  55. <soap:address location=“http://localhost:7777/tudou”/>  
  56. port>  
  57. service>  
  58. definitions>  

[html]  view plain  copy

 print ?

[html]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

  1. package com.whaty.platform.ws.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5.   
  6. import javax.xml.namespace.QName;  
  7. impor t javax.xml.ws.Service;  
  8.   
  9. import com.whaty.platform.ws.server.IMyservice;  
  10.   
  11. /** 
  12.  * @className:MyClient.java 
  13.  * @Desc:访问发布的服务 
  14.  * @author:lizhuang 
  15.  * @createTime:2012-12-21 上午01:23:57 
  16.  */  
  17. public class MyClient {  
  18.     public static void main(String[] args) {  
  19.   
  20.         try {  
  21.             //服务WSDL Document的地址  
  22.             URL url = new URL(“http://localhost:7777/tudou?wsdl”);  
  23.             //Qnameqname是qualified name 的简写< /span>  
  24.             //2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成  
  25.             //由发布的wsdl可知namespace为http://server.ws.platform.whaty.com/,  
  26.             QName qname=new QName(“http://server.ws.platform.whaty.com/”,“MyServiceImplService”);  
  27.             Service service=Service.cre ate(url, qname);  
  28.             IMyservice ms=service.getPort(IMyservice.class);  
  29.             ms.add(14);  
  30.             ms.minus(14);  
  31.         } catch (Malfor medURLException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35. }  

[java]  view plain  copy

 print ?

[java]  view plain  copy

 print ?

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3477 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.