webservice的学习笔记(一)

来源:互联网 发布:购物分享php系统 编辑:程序博客网 时间:2024/05/28 03:01
一:webService :异构平台之间的交互。 CXF  Axis    Metro  框架    java中的框架  jax-ws

   1.xml解析的方法有:(DTD, schame ,  Stax)

    2.SOAP

    3.jax-ws(java  api  xml  webService)

     4.契约优先的开发模式:

SEI (service   Endpoint   Interface )   SIB (Service    implements    Bean )

二:遇到的代码:

 1.简单的webservice项目:

   1):创建一个简单的接口。

       

package webservice.test;

@WebService

public interface IMyservice {
 
 
    public  int  add(int a,int b);
 
     public  int  minus(int a,int b);

}



    2):创建实现类。

package webservice.test;

import javax.jws.WebService;

@WebService(endpointInterface="webservice.test.IMyservice")
public class IMyserviceImp implements  IMyservice{

 @Override
 public int add(int a, int b) {
  System.out.println(a+"+"+b+"="+(a+b));
  return a+b;
 }

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





    3):创建服务.

package webservice.test;

import javax.xml.ws.Endpoint;


public class MyService {
 public static void main(String[] args) {

  String address="http://localhost:6666/ns";
     Endpoint.publish(address,new IMyserviceImp());
 }
 
}


运行后访问网址:http://localhost:6666/ns?wsdl 然后会找到对应的提供xml


<?xml version="1.0" encoding="UTF-8"?>

<!-- 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 name="IMyserviceImpService" targetNamespace="http://test.webservice/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://test.webservice/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> -<types> +<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="IMyservice"> -<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="IMyserviceImpPortBinding" type="tns:IMyservice"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> -<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="IMyserviceImpService"> -<port name="IMyserviceImpPort" binding="tns:IMyserviceImpPortBinding"> <soap:address location="http://localhost:6666/ns"/> </port> </service> </definitions>


4.创建一个客户端:
package webservice.test;

import java.net.MalformedURLException;
import java.net.URL;

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

public class TestClient {
    
    public static void main(String[] args) {
        try {
            URL   url=new URL("http://localhost:6666/ns?wsdl");
            QName  qname=new QName("http://test.webservice/","IMyserviceImpService");
            Service   service= Service.create(url,qname);
            
            IMyservice  ms=service.getPort(IMyservice.class);
            System.out.println(ms.add(12, 44));
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }
这样就可以调用服务。


由于我们调用别人的接口,我们不知道他的实现类,所以我们应该用另一种方式:



二:wsimport   只要是JAva6版本以上都可以有这个命令。

可以通过的导出生成的接口:
 wsimport  -d  目录名  -keep(指定是否生成源文件.java)  -verbose(详细信息过程)  需要的wsdl服务
 

 会通过导出的文件,然后根据这些文件找到对应的类与接口,然后调用里面的方法。实现结果。


0 0