随笔 - 119  文章 - 3173  trackbacks - 0
<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

交友莫独酒,茅台西凤游。
口干古井贡,心徜洋河流。
称多情杜康,趟无量双沟。
赞中华巍巍,无此不销愁。

常用链接

留言簿(68)

随笔分类(136)

随笔档案(122)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 520466
  • 排名 - 93

最新评论

  Java6发布了,其中一个吸引我的新特性就是原生支持WebServices。在这和大家分享下学习心得。
下面就开始写个最简单的WebServices:
package org.hermit.study.jdk;

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

@WebService(targetNamespace 
= "http://jdk.study.hermit.org/client")
@SOAPBinding(style 
= SOAPBinding.Style.RPC)
public class Hello {
    @WebMethod
    
public String sayHello(String name) {
        
return "hello:" + name;
    }
}
怎么样简洁吧,很多朋友的写法还要在命令行中执行“wsgen –cp . <path>
用偶这种方法写的service可以省去上面这步。

targetNamespace = "http://jdk.study.hermit.org/client"这句是指定客户端获取服务端服务后存放的类路径。注意是反着的,http: //jdk.study.hermit.org/client在客户端生成的类会放在org.hermit.study.jdk.client包下。
下面是发布服务:
package org.hermit.study.jdk;

import javax.xml.ws.Endpoint;

public class StartService {
    
public static void main(String[] args) {
        Endpoint.publish(
"http://localhost:8080/HelloService"new Hello());
    }

}
呵呵,更简洁。一句话而已。
http://localhost:8080/HelloService是指发布的地址

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



如果能看到以下内容,就可以
 <?xml version="1.0" encoding="UTF-8" ?> 
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://jdk.study.hermit.org/client" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://jdk.study.hermit.org/client" 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 style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
<operation name="sayHello">
  
<soap:operation soapAction="" /> 
<input>
  
<soap:body use="literal" namespace="http://jdk.study.hermit.org/client" /> 
  
</input>
<output>
  
<soap:body use="literal" namespace="http://jdk.study.hermit.org/client" /> 
  
</output>
  
</operation>
  
</binding>
<service name="HelloService">
<port name="HelloPort" binding="tns:HelloPortBinding">
  
<soap:address location="http://localhost:8080/HelloService" /> 
  
</port>
  
</service>
  
</definitions>
posted on 2006-12-21 10:01 交口称赞 阅读(6287) 评论(6)  编辑  收藏 所属分类: Java6

FeedBack:
# re: Java6 WebServices (一)服务端 2006-12-21 21:45 关关
下篇呢  回复  更多评论
  
# re: Java6 WebServices (一)服务端 2006-12-25 00:32 Shawn
看起来很cool,我是JAVA的初学者,我以SERVLET的方式用web.xml的形式放到TOMCAT下但是运行要出错,是怎么回事呢?  回复  更多评论
  
# re: Java6 WebServices (一)服务端 2006-12-25 13:11 交口称赞
@Shawn
不知道你的具体代不好解决,
可能你的TOMCAT不支持JDK6的新语法。。。。。。。用6.x的试试
也可能不支持SERVLET方式发布。。。。。。。。。

  回复  更多评论
  
# re: 学习Java6(一) WebServices (1)服务端 2007-01-05 13:46 交口称赞
# re: 学习Java6(一) WebServices (1)服务端[未登录] 2007-01-26 10:28 OLIVE
在你class Hello里边放多个@WebMethod ,也就是发布多个服务时,怎么会报jaxws.ExceptionBean异常。请问怎么解决。  回复  更多评论
  
# re: 学习Java6(一) WebServices (1)服务端 2007-10-28 14:39 zhaoyi4374
请问如何返回一个对象啊??  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: