您的位置:首页 > 编程语言 > Java开发

使用JDK编写webService

2015-11-12 21:53 591 查看
在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;

@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);

}


2、实现webService服务器代码

创建类HelloWSImpl,并实现HelloWS

HelloWSImpl.java

package com.study.ws_server;

import javax.jws.WebService;

@WebService
public class HelloWSImpl implements HelloWS {

@Override
public 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: