java6 WebServices服务端

java6 发布后,吸引人就是一个新特性WebService,下面我来跟大分享一下

下面是一个最简单的java6 WebService

新一个项目webservice,Hello.java源代码

package com.cissco.serive;

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

@WebService(targetNamespace = "http://www.cissco.cn")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class Hello {

@WebMethod
public String sayHello(String name) {
   System.out.println(name);// 用来测试sayHello方法是否被调用
   return name + " Say Hello!";
}
}

 

//targetNamespace = "http://www.cissco.cn"这句是指定客户端获取服务端服务后存放的类路径。注意是反着的,http://www.cissco.cn在客户端生成的类会放在cn.cissco包

下面是发布服务StartService.java源代码

package com.cissco.serive;

import javax.xml.ws.Endpoint;

public class StartService {

/**
* @param args
*/
public static void main(String[] args) {
   // TODO Auto-generated method stub
   Endpoint.publish("http://localhost:5050/HelloService",new Hello());
  
   System.out.println("WebService已经发布成功!");
}
}

 

//呵呵,更简单吧。一句话而已。
http://localhost:5050/HelloService是指发布的地址

运行
StartService ,开发浏览器输入:http://localhost:5050/HelloService?wsdl

如果能看到以下xml内容,证明HelloService发布成功

<?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.1 in JDK 6. --> 
 <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> 
 <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.cissco.cn" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.cissco.cn" name="HelloService">
    <types />
    <message name="sayHello">
       <part name="arg0" type="xsd:string" />
    </message>
    <message name="sayHelloResponse">
       <part name="return" type="xsd:string" />
    </message>
       <portType name="Hello">
          <operation name="sayHello" parameterOrder="arg0">
             <input message="tns:sayHello" />
             <output message="tns:sayHelloResponse" />
          </operation>
       </portType>
       <binding name="HelloPortBinding" type="tns:Hello">
          <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
          <operation name="sayHello">
             <soap:operation soapAction="" />
             <input>
                <soap:body use="literal" namespace="http://www.cissco.cn" />
             </input>
             <output>
                <soap:body use="literal" namespace="http://www.cissco.cn" />
             </output>
          </operation>
       </binding>
       <service name="HelloService">
          <port name="HelloPort" binding="tns:HelloPortBinding">
	     <soap:address location=http://localhost:5050/HelloService />
          </port>
        </service>
</definitions>

 

 

 

你可能感兴趣的:(jdk,xml,.net,webservice,SOAP)