使用JDK编写webService
来源:互联网 发布:mac的dock怎么隐藏 编辑:程序博客网 时间:2024/05/22 04:36
在JDK1.6以及以上版本中,已经加入了原生的webService的支持,通过简单的编写就可以实现一个java webService
一、编写服务器代码
1、定义SEI(Service Endpoint Interface 发布的服务接口)
首先,创建一个普通的java project,命名为ws_server,并在src下创建一个包,命名为com.study.ws_server
之后创建名称为HelloWS的接口,其中内容如下:
HelloWS.java
package com.study.ws_server;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface HelloWS {@WebMethodpublic String sayHello(String name);}
2、实现webService服务器代码
创建类HelloWSImpl,并实现HelloWS
HelloWSImpl.java
package com.study.ws_server;import javax.jws.WebService;@WebServicepublic class HelloWSImpl implements HelloWS {@Overridepublic String sayHello(String name) {System.out.println("com.study.ws_server.HelloWSImpl: " + name);return "Hello " + name;}}
这里仅仅是接收到的参数简单处理一下就返回了
3、发布webService
创建包com.study.ws_server.server,并创建类ServerTest
ServerTest.java
package com.study.ws_server.server;import javax.xml.ws.Endpoint;import com.study.ws_server.HelloWS;import com.study.ws_server.HelloWSImpl;public class ServerTest {public static void main(String[] args) {String address = "http://localhost:8888/ws_server";HelloWS helloWS = new HelloWSImpl();Endpoint.publish(address, helloWS);System.out.println("ServerTest publish success!");}}
编写完成,运行ServerTest.java,控制台提示成功后,在浏览器输入http://localhost:8888/ws_server?wsdl,得到的内容如下:
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws_server.study.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws_server.study.com/" name="HelloWSImplService"><types><xsd:schema><xsd:import namespace="http://ws_server.study.com/" schemaLocation="http://localhost:8888/ws_server?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="HelloWSImpl"><operation name="sayHello"><input wsam:Action="http://ws_server.study.com/HelloWSImpl/sayHelloRequest" message="tns:sayHello"/><output wsam:Action="http://ws_server.study.com/HelloWSImpl/sayHelloResponse" message="tns:sayHelloResponse"/></operation></portType><binding name="HelloWSImplPortBinding" type="tns:HelloWSImpl"><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="HelloWSImplService"><port name="HelloWSImplPort" binding="tns:HelloWSImplPortBinding"><soap:address location="http://localhost:8888/ws_server"/></port></service></definitions>根据上面的内容可以生成客户端代码
二、编写客户端代码
1、根据WSDL文件生成客户端代码
创建java project 命名为ws_client
鼠标右键src,选择terminal
在弹出的终端中输入wsimport -keep http://localhost:8888/ws_server?wsdl,并回车
等待提示代码生成完毕后刷新src,可以看到自动生成的代码
2、调用服务器webService接口
创建包com.study.ws_client.client,并创建类ClientTest
ClientTest.java
package com.study.ws_client.client;import com.study.ws_server.HelloWSImpl;import com.study.ws_server.HelloWSImplService;public class ClientTest {public static void main(String[] args) {HelloWSImplService service = new HelloWSImplService();HelloWSImpl helloWSImplPort = service.getHelloWSImplPort();String result = helloWSImplPort.sayHello("Andy");System.out.println("result content: " + result);}}编写完毕后,运行ClientTest,控制台显示结果为:
result content: Hello Andy
- 使用JDK编写webService
- WebService:编写webService使用实例
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用JDK开发WebService
- 使用jdk开发webservice
- 利用jdk使用WebService
- 使用JDK开发WebService
- 使用Axis编写WebService
- 使用CXF编写WebService
- webservice编写和使用
- 【webservice开发】之使用JDK开发WebService
- 使用jdk自带webservice发布webservice
- WebService 一 (使用JDK实现WebService)
- java应用一般架构
- PostgreSQL学习手册(常用数据类型)
- 606第九周周六赛 A - 免费馅饼
- [kuangbin带你飞]专题一 简单搜索 C - Catch That Cow poj 3278
- tomcat下jndi的三种配置方式
- 使用JDK编写webService
- HDU 4911 归并排序题目解析
- 2015.11.12 为什么还可以接着输入
- 时隔一年我又回来写博客。
- PostgreSQL学习手册(函数和操作符<一>)
- OpenSessionInViewFilter不起作用问题
- RecyclerView的局部刷新
- HDOJ--1269--迷宫城堡
- 11 编程指南_流数据