快速实现一个基于jws的webservice项目

一.服务器的建立
     1.创建服务接口
         我们常听说的 SEI(server endpoint intelface) 就是这的这个服务的接口
         
          package webservice.wb01;
import javax.jws.WebService;
@WebService
public interface IMyserver {
        public int add(int a, int b);
        public int minus(int a,int b);
}




     2.创建服务的实现类 SIB(server implements  bean)
         package webservice.wb01;
import javax.jws.WebService;
@WebService(endpointInterface="webservice.wb01.IMyserver" )
public class MyserverImpl implements IMyserver  {

       
        public int add(int a, int b) {
              System. out.println("add" );
               return a+b;
       }
       
        public int minus(int a, int b) {
              System. out.println("minus" );
               return a-b;
       };
}


     3.发布服务
          package webservice.wb01;
import javax.xml.ws.Endpoint;
public class Test {

        public static void main(String[] args) {
              String address= "http://localhost:9090/ns";
              Endpoint. publish(address, new MyserverImpl());
       }
}

    。
   在浏览器中访问http://localhost:9090/ns?wsdl  就可以看的服务运行的情况

<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace= "http://wb01.webservice/" name="MyserverImplService" >
        <types>
               <xsd:schema>
                      <xsd:import namespace= "http://wb01.webservice/" schemaLocation="http://localhost:9090/ns?xsd=1" />
               </xsd:schema>
        </types>
        <message name= "minus">
               <part name= "parameters" element= "tns:minus" />
        </message>
        <message name= "minusResponse">
               <part name= "parameters" element= "tns:minusResponse" />
        </message>
        <message name= "add">
               <part name= "parameters" element= "tns:add" />
        </message>
        <message name= "addResponse">
               <part name= "parameters" element= "tns:addResponse" />
        </message>
        <portType name= "IMyserver">
               <operation name= "minus">
                      <input message= "tns:minus"/>
                      <output message= "tns:minusResponse"/>
               </operation>
               <operation name= "add">
                      <input message= "tns:add"/>
                      <output message= "tns:addResponse"/>
               </operation>
        </portType>
        <binding name= "MyserverImplPortBinding" type= "tns:IMyserver" >
               <soap:binding transport= "http://schemas.xmlsoap.org/soap/http" style="document" />
               <operation name= "minus">
                      <soap:operation soapAction= ""/>
                      <input>
                            <soap:body use= "literal"/>
                      </input>
                      <output>
                            <soap:body use= "literal"/>
                      </output>
               </operation>
               <operation name= "add">
                      <soap:operation soapAction= ""/>
                      <input>
                            <soap:body use= "literal"/>
                      </input>
                      <output>
                            <soap:body use= "literal"/>
                      </output>
               </operation>
        </binding>
        <service name= "MyserverImplService">
               <port name= "MyserverImplPort" binding= "tns:MyserverImplPortBinding" >
                      <soap:address location= "http://localhost:9090/ns"/>
               </port>
        </service>
</definitions>




二。创建客户端
     package webservice.wb01;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class ClientTest {
        public static void main(String[] args) {
               try {
                     URL url= new URL("http://localhost:9090/ns" );
                                   /***********************************************************
                                        <definitions targetNamespace="http://wb01.webservice/" name="MyserverImplService">

                                       QName中的第一个参数为wsdl中 targetNamespace的值,第二个参数为:name的值 
                                      ***** ***********************************************/
                     QName qname= new QName("http://wb01.webservice/" , "MyserverImplService" );
                     Service service=Service. create(url, qname);
                     IMyserver iMyserver= service.getPort(IMyserver. class );
                     
                     System. out .println("1+2= " +  iMyserver.add(1, 2));
                     System. out .println("5-2= " +  iMyserver.minus(5, 2));
                     
              } catch (Exception e) {
              
                     e.printStackTrace();
              }
       }

}














你可能感兴趣的:(java,webservice,SOAP,jws)