JAX-WS 简单可运行例子简介

 

概念

JAX-WS2.0的全称Java API for XML-Based Web Service 2.0.JAX-WS2.0是对JAX-RPC1.0规范的扩展,是JAX-RPC1.1的后续版本,JAX-RPC2.0标准发布不久后就被重命名为JAX-WS2.0.

JAX-WS2.0是Sun新的Web service协议栈,是一个完全基于标准实现的。在binding层,使用的是the Java Architecture for XML Binding(JAXB),在parsing层,使用的是the Streaming API for XML(StAX),同时它还完全支持schema规范。


术语

  • SEI:Service Endpoint Interface
  • JAX:Java API for XML Web Servcie
  • JAX-WS RI:JAX-WS Reference Implementation
  • SAAJ:SOAP with Attachments API for Java

创建 Web Service方式

JAX-WS 2.0有两种创建Web Service的开发过程:

  • 从Java类出发创建Web Service
  • 发布Web Service
  • 通过wsgen工具,根据发布成功的wsdl生成客户端代码
  • 调用客户端代码访问webservice

Eclipse jdk 6.0

 

package com.zzz.jaxws.service;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * @description
 */
@WebService(serviceName = "HelloService", portName = "HelloServicePort", targetNamespace = "http://jaxws.zzz/jaxws/hello")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class HelloService {
	@WebMethod
	public String sayHello(String s) {
		System.out.println("hello," + s);
		return s;
	}
}

进入工程目录:

mkdir wsdl

wsgen -cp ./bin -r ./wsdl -s ./src -d ./bin -wsdl com.zzz.jaxws.service.HelloService

刷新工程 可以看到

wsdl 下有HelloService.wsdl

或者可以直接使用java代码发布:

 

 

package com.zzz.jaxws.service;
import javax.xml.ws.Endpoint;
import com.zzz.jaxws.service.HelloService;
/**
* @description 发布HelloService.java为服务
*/
public class HelloServicePublisher {
public void publish(){
   Endpoint.publish("http://localhost:8080/services/HelloService",
     new HelloService());
}
public static void main(String[] args){
   HelloServicePublisher publish = new HelloServicePublisher();
   publish.publish();
}
}

 

 

访问: http://localhost:8080/services/HelloService?wsdl 

 

<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://jaxws.zzz/jaxws/hello" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jaxws.zzz/jaxws/hello" name="HelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://jaxws.zzz/jaxws/hello" schemaLocation="http://localhost:8080/services/HelloService?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="HelloService">
<operation name="sayHello">
<input message="tns:sayHello"/>
<output message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://localhost:8080/services/HelloService"/>
</port>
</service>
</definitions>

 发布成功!

 

通过wsdl生成客户端代码:

wsimport -keep -p com.zzz.jaxws.service.client http://localhost:8080/services/HelloService?wsdl

刷新工程可以看到

com.zzz.jaxws.service.client目录下有client的代码,copy到src目录下。

package com.zzz.jaxws.service.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;

/**
* @description 
* @version 创建时间:Apr 3, 2009 9:49:41 AM
*/
public class HelloServiceClient {
private final QName qName=new QName("http://jaxws.zzz/jaxws/hello","HelloService");

public static URL getWSDLURL(String urlStr){
   URL url = null;
   try {
    url = new URL(urlStr);
   } catch (MalformedURLException e) {
    e.printStackTrace();
   }
   return url;
}

public void process(URL url){
   HelloService_Service service = new HelloService_Service(url,qName);
   HelloService port = service.getHelloServicePort();
   String response = port.sayHello("world");
   System.out.println("result:"+response);
}

/**
* @param args
 * @throws MalformedURLException 
*/
public static void main(String[] args) throws MalformedURLException {
/*   if(args.length!=1){
    System.out.println("Please enter parameter.");
    return;
   }*/
//   URL url = getWSDLURL(args[0]);
	URL url = new URL("http://localhost:8080/services/HelloService?wsdl");
   HelloServiceClient client = new HelloServiceClient();
   client.process(url);
}
}

 

调用客户段测试代码,看到结果:

result:world

 

你可能感兴趣的:(jax-ws)