JDK开发WebService
2024-09-04 13:06:15
java开发web service最简单的方式是用jdk6自带的支持web service的注解功能。
1、编写代码如下:
package net.swiftlet; import javax.jws.WebService;
import javax.xml.ws.Endpoint; //targetNamespace的默认值是service类所在包名,也可以设置成发布service的网站域名
@WebService(serviceName = "HelloService", targetNamespace = "www.swiftlet.net")
public class HelloService {
public String sayhello(String name) {
return "Hello " + name + " !"; } public static void main(String[] args) {
// 发布web service,参数1是服务公开的访问地址address,参数2是服务的具体实现
Endpoint.publish("http://localhost:8080/hello", new HelloService());
}
}
2、运行代码,一般以java application运行方式即可。可以看到运行后进程和启ServerSocket服务程序一样一直出于开启状态。输出信息如下:
2015-4-2 20:05:16 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class net.swiftlet.jaxws.Sayhello
2015-4-2 20:05:16 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class net.swiftlet.jaxws.SayhelloResponse
3、在host系统文件里添加"127.0.0.1 www.swiftlet.net"。打开浏览器,输入"http://www.swiftlet.net:8080/hello",回车,此时页面可能是
Web Services No JAX-WS context information available.
因为一般浏览器不能直接访问WS程序,不过所有的WS都可以生成WSDL文档供一般浏览器访问,方式是在地址后面加上“?wsdl”就可以了,http://www.swiftlet.net:8080/hello?wsdl。
生成的文档如下:
<definitions targetNamespace="www.swiftlet.net" name="HelloService">
<types><xsd:schema><xsd:import namespace="www.swiftlet.net" schemaLocation="http://www.swiftlet.net:8080/hello?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://www.swiftlet.net:8080/hello"/>
</port>
</service>
</definitions>
4 除了可以在浏览器上访问发布的service程序,还可以通过客户端来获取。如下代码:
package net.swiftlet;
public class WebServiceClient
{
public static void main(String[] args)
{
HelloService myWebService = new HelloService();
System.out.println(myWebService.sayhello("Swiftlet"));
}
}
输出结果为:
Hello Swiftlet !
5 最后想说一点:HelloService类的@WebService注解部分可以简化。
@WebService()
public class HelloService
{
public String sayhello(String name){
return "Hello " + name + " !";
}
public static void main(String[] args){
//发布web service,参数1是服务公开的访问地址address,参数2是服务的具体实现
Endpoint.publish("http://localhost:8080/hello", new HelloService());
}
}
这样就不用配置host:"127.0.0.1 www.swiftlet.net",可以使用此链接直接在浏览器进行访问:http://localhost:8080/hello?wsdl
最新文章
- clipChildren和clipToPadding
- JDBC的操作总结
- selenium之 定位以及切换frame(iframe)
- Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据
- HTML5中的SVG
- Linux Bash代码 利用for循环实现命令的多次执行
- WebsitePanel 2.1.0beta配置部分
- HDU 2176 (Nim博弈 先手取胜方案) 取(m堆)石子游戏
- C# 连接 Oracle 的几种方式[转]
- easyui 很好很强大
- pfile,spfile 初始化参数文件顺序【weber出品】
- C#如何使SQLite程序集既能适应32位系统也能适应64位系统
- Atom读写MarkDown插件选择,以及墙内安装markdown-preview-enhanced,及markdown和mermaid使用教程
- 根据要求写SQL 语句
- 线程Thread类
- mac 比较两个文件
- 搭建RabbitMQ集群(Docker)
- 【转】10个非常有用的网页设计工具 | Goodfav Magazine
- ajax和SSH请求中乱码的一些解决方法
- Blend for Visual Studio 2013