Web Services

三元素: SOAP,WSDL,UDDI

  1. soap 传输协议 包括envelope, header, bodyExample SOAP Request Example SOAP Request
    <?xml version=”1.0″?> <?xml version=”1.0″?>
    <soap:Envelope <soap:Envelope
    xmlns:soap=”http://www.w3.org/2001/12/soap xmlns:soap=”http://www.w3.org/2001/12/soap&#8211;
    envelope” envelope”
    soap:encodingStyle=”http://www.w3.org/2001/12/ soap:encodingStyle=”http://www.w3.org/2001/12/
    soap soap–encoding”> encoding”>
    <soap:Body <soap:Body
    xmlns:m=”http://www.example.org/stock”&gt; xmlns:m=”http://www.example.org/stock”&gt;
    <m:GetStockPrice> <m:GetStockPrice>
    <m:StockName>IBM</m:StockName> <m:StockName>IBM</m:StockName>
    </m:GetStockPrice> </m:GetStockPrice>
    </soap:Body> </soap:Body>
    </soap:Envelope>

    Example SOAP Response Example SOAP Response
    <?xml version=”1.0″?> <?xml version=”1.0″?>
    <soap:Envelope <soap:Envelope
    xmlns:soap=”http://www.w3.org/2001/12/soap xmlns:soap=”http://www.w3.org/2001/12/soap–envelope&#8221; envelope”
    soap:encodingStyle=”http://www.w3.org/2001/12/soap soap:encodingStyle=”http://www.w3.org/2001/12/soap-
    encoding”> encoding”>
    <soap:Body xmlns:m=”http://www.example.org/stock”&gt; <soap:Body xmlns:m=”http://www.example.org/stock”&gt;
    <m:GetStockPriceResponse> <m:GetStockPriceResponse>
    <m:Price>34.5</m:Price> <m:Price>34.5</m:Price>
    </m:GetStockPriceResponse> </m:GetStockPriceResponse>
    </soap:Body> </soap:Envelope>

  2. wsdl 描述服务 包括<types>: Data types used
    <message>: Messages that are used by the service
    <portType>:operations that are supported
    <binding>:Description of network protocol for invocation
    <service>: Reference to actual location of service
  3. uddi 发布服务

 

Creating JAX Creating JAX–RPC Services RPC Services
1.1. Write the Service Endpoint Interface (SEI), Write the Service Endpoint Interface (SEI),
and service implementation and service implementation
2.2. Configure Deployment Descriptor (as a Servlet Configure Deployment Descriptor (as a Servlet
or EJB) or EJB)
3.3. Create WSDL and Mapping XML files (Use Create WSDL and Mapping XML files (Use
command line tools, or Ant) command line tools, or Ant)
4.4. Create webservices.xml file to glue the WSDL Create webservices.xml file to glue the WSDL
and Mapping files together and Mapping files together
5.5. Deploy the service (as a WAR or JAR)

更简单方法: JAX-WS

用@WebService和@WebMethod  , IDE生成WSDL

 

用cxf的ServiceFactory更简单“”:

1.接口文件:
package javaws;

import javax.jws.WebService;

@WebService
public interface HelloWorld
{
 public String SayHello(String name);
}
2.实现接口文件:
package javaws;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld
{

  public String SayHello(String name)
  {
    System.out.println(“sevice is called!”);
    return “hello ,”+name;
  }

}
3.服务器端:
package javaws;
import org.apache.cxf.frontend.ServerFactoryBean;

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

     HelloWorldImpl helloworldImpl = new HelloWorldImpl();
     ServerFactoryBean svrFactory = new ServerFactoryBean();
     svrFactory.setServiceClass(HelloWorld.class);
     svrFactory.setAddress(“http://localhost:8888/Hello”); 
     svrFactory.setServiceBean(helloworldImpl);
     svrFactory.create();

  }

}
4.客户端:
package javaws;
import org.apache.cxf.frontend.ClientProxyFactoryBean;

public class TestClient
{

  public static void main(String[] args)
  {

     ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
     factory.setServiceClass(HelloWorld.class);
     factory.setAddress(“http://localhost:8888/Hello”); 
     HelloWorld client = (HelloWorld)factory.create();
     System.out.println(“Invoke sayHi()….”);
     System.out.println(client.SayHello(“tiger”));

  }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s