webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)

1.定义接口

 1 package org.WebService.ws.annotation;
 2 
 3 import javax.jws.WebService;
 4 
 5 @WebService
 6 public interface ICalculator {
 7     float add(float a, float b);
 8     float minus(float a, float b);
 9     float multiply(float a, float b);
10     float divide(float a, float b);
11 }

2.服务接口实现类

 1 package org.WebService.ws.annotation;
 2 
 3 import javax.jws.WebService;
 4 //endpointInterface指定接入点接口:接口必须存在 
 5 @WebService(endpointInterface="org.WebService.ws.annotation.ICalculator")
 6 public class CalculatorImpl implements ICalculator {
 7 
 8     @Override
 9     public float add(float a, float b) {
10         // TODO Auto-generated method stub
11         System.out.println("a+b="+(a+b));  
12         return a + b;
13     }
14 
15     @Override
16     public float minus(float a, float b) {
17         // TODO Auto-generated method stub
18         System.out.println("a-b="+(a-b)); 
19         return a - b;
20     }
21 
22     @Override
23     public float multiply(float a, float b) {
24         // TODO Auto-generated method stub
25         return a * b;
26     }
27 
28     @Override
29     public float divide(float a, float b) {
30         // TODO Auto-generated method stub
31         return a / b;
32     }
33 
34 }

3.发布服务

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

 1 package org.WebService.ws.annotation;
 2 
 3 import javax.xml.ws.Endpoint;
 4 
 5 public class Server {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         String address="http://localhost:9998/hw";
10         Endpoint.publish(address, new CalculatorImpl());
11     }
12 
13 }

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

http://localhost:9998/hw?wsdl

浏览器显示如下:

  1 This XML file does not appear to have any style information associated with it. The document tree is shown below.
  2 <!--
  3  Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
  4 -->
  5 <!--
  6  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
  7 -->
  8 <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://annotation.ws.WebService.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://annotation.ws.WebService.org/" name="CalculatorImplService">
  9 <types>
 10 <xsd:schema>
 11 <xsd:import namespace="http://annotation.ws.WebService.org/" schemaLocation="http://localhost:9998/hw?xsd=1"/>
 12 </xsd:schema>
 13 </types>
 14 <message name="add">
 15 <part name="parameters" element="tns:add"/>
 16 </message>
 17 <message name="addResponse">
 18 <part name="parameters" element="tns:addResponse"/>
 19 </message>
 20 <message name="multiply">
 21 <part name="parameters" element="tns:multiply"/>
 22 </message>
 23 <message name="multiplyResponse">
 24 <part name="parameters" element="tns:multiplyResponse"/>
 25 </message>
 26 <message name="divide">
 27 <part name="parameters" element="tns:divide"/>
 28 </message>
 29 <message name="divideResponse">
 30 <part name="parameters" element="tns:divideResponse"/>
 31 </message>
 32 <message name="minus">
 33 <part name="parameters" element="tns:minus"/>
 34 </message>
 35 <message name="minusResponse">
 36 <part name="parameters" element="tns:minusResponse"/>
 37 </message>
 38 <portType name="ICalculator">
 39 <operation name="add">
 40 <input wsam:Action="http://annotation.ws.WebService.org/ICalculator/addRequest" message="tns:add"/>
 41 <output wsam:Action="http://annotation.ws.WebService.org/ICalculator/addResponse" message="tns:addResponse"/>
 42 </operation>
 43 <operation name="multiply">
 44 <input wsam:Action="http://annotation.ws.WebService.org/ICalculator/multiplyRequest" message="tns:multiply"/>
 45 <output wsam:Action="http://annotation.ws.WebService.org/ICalculator/multiplyResponse" message="tns:multiplyResponse"/>
 46 </operation>
 47 <operation name="divide">
 48 <input wsam:Action="http://annotation.ws.WebService.org/ICalculator/divideRequest" message="tns:divide"/>
 49 <output wsam:Action="http://annotation.ws.WebService.org/ICalculator/divideResponse" message="tns:divideResponse"/>
 50 </operation>
 51 <operation name="minus">
 52 <input wsam:Action="http://annotation.ws.WebService.org/ICalculator/minusRequest" message="tns:minus"/>
 53 <output wsam:Action="http://annotation.ws.WebService.org/ICalculator/minusResponse" message="tns:minusResponse"/>
 54 </operation>
 55 </portType>
 56 <binding name="CalculatorImplPortBinding" type="tns:ICalculator">
 57 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
 58 <operation name="add">
 59 <soap:operation soapAction=""/>
 60 <input>
 61 <soap:body use="literal"/>
 62 </input>
 63 <output>
 64 <soap:body use="literal"/>
 65 </output>
 66 </operation>
 67 <operation name="multiply">
 68 <soap:operation soapAction=""/>
 69 <input>
 70 <soap:body use="literal"/>
 71 </input>
 72 <output>
 73 <soap:body use="literal"/>
 74 </output>
 75 </operation>
 76 <operation name="divide">
 77 <soap:operation soapAction=""/>
 78 <input>
 79 <soap:body use="literal"/>
 80 </input>
 81 <output>
 82 <soap:body use="literal"/>
 83 </output>
 84 </operation>
 85 <operation name="minus">
 86 <soap:operation soapAction=""/>
 87 <input>
 88 <soap:body use="literal"/>
 89 </input>
 90 <output>
 91 <soap:body use="literal"/>
 92 </output>
 93 </operation>
 94 </binding>
 95 <service name="CalculatorImplService">
 96 <port name="CalculatorImplPort" binding="tns:CalculatorImplPortBinding">
 97 <soap:address location="http://localhost:9998/hw"/>
 98 </port>
 99 </service>
100 </definitions>

4.创建客户端

 1 package org.WebService.ws.annotation;
 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 public class Client {
10 
11     public static void main(String[] args) throws MalformedURLException {
12         // TODO Auto-generated method stub
13         URL url=new URL("http://localhost:9998/hw?wsdl");
14         QName qname = new QName("http://annotation.ws.WebService.org/", "CalculatorImplService");
15         Service service = Service.create(url, qname);
16         ICalculator cal = service.getPort(ICalculator.class);
17         cal.add(1, 2);
18         cal.minus(2, 1);
19         
20     }
21 
22 }

 为了简化,本例中客户端和服务器是在同一个工程中的,实际上需要用Eclipse把ICalculator接口导出成jar包在导入到客户端工程中即可,客户端代码保持不变。这里也可以使用jdk提供的wsimport命令导出服务端jar包,续。。。

posted on 2015-10-16 22:31  ilinux_one  阅读(1959)  评论(0编辑  收藏  举报

导航