物流网站建设规划书,腾讯网站开发,上海企业信息查询,如何获得企业邮箱JAX-WS#xff0c;即Java API for XML Web Service#xff0c;是Java开发基于SOAP协议的Web Service的标准。使用JWS API就可以直接开发简单的Web Service应用。 一、创建Web Service 打开Eclipse#xff0c;新建一个Java Project#xff0c;如下图所示#xff1a; 新建了…JAX-WS即Java API for XML Web Service是Java开发基于SOAP协议的Web Service的标准。使用JWS API就可以直接开发简单的Web Service应用。 一、创建Web Service 打开Eclipse新建一个Java Project如下图所示 新建了“HelloWorld”一个接口“User”、“HelloWorldImpl”、“JWSWebService”三个类其中“User”是实体类“HelloWorldImpl”继承自“HelloWorld”接口“JWSWebService”类是主程序入口。 “HelloWorld”接口的代码如下 package com.guowei.ws.jws;import javax.jws.WebParam;
import javax.jws.WebService;/*** author guowei**/
WebService
public interface HelloWorld {String sayHi(WebParam(nametext) String text);String sayHiToUser(WebParam(nameuser) User user);
}“User”类的代码如下 package com.guowei.ws.jws;/*** author guowei**/
public class User {private String name;private String description;public String getName() {return name;}public void setName(String name) {this.name name;}public String getDescription() {return description;}public void setDescription(String description) {this.description description;}
}“HelloWorldImpl”类的代码如下 package com.guowei.ws.jws;import javax.jws.WebService;WebService(endpointInterface com.guowei.ws.jws.HelloWorld,serviceName HelloWorld)
public class HelloWorldImpl implements HelloWorld {public String sayHi(String text) {// TODO Auto-generated method stubSystem.out.println(sayHi called);return Hello text;}public String sayHiToUser(User user) {// TODO Auto-generated method stubSystem.out.println(sayHiToUser called);return Hello user.getName() user.getDescription();}}“JWSWebService”类的代码如下 package com.guowei.ws.jws;import javax.xml.ws.Endpoint;public class JWSWebService {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println(Starting Server);HelloWorldImpl implementor new HelloWorldImpl();String address http://localhost:9000/helloWorld;Endpoint.publish(address, implementor);System.out.println(Web Service started);} }二、发布Web Service 右键点击项目在弹出的菜单中选择“Run As”-“Java Application”如下图所示 控制台出现如下提示表明Web Service发布成功。 在浏览器地址栏中输入地址http://localhost:9000/helloWorld?wsdl出现如下界面说明Web Service发布成功了。其中的内容则是Web Service的描述信息。 三、Java客户端调用Web Service Java客户端调用Web Service首先需要wsimport工具生成客户端代码wsimport工具是jdk自带的工具用于根据WSDL文件自动生成本地的Java类。wsimport在jdk安装目录下的bin目录下如果配置好了Java环境则可以在控制台以命令直接使用。 wsimport工具的使用详见使用wsimport命令创建Web Service客户端。 生成的类文件如下 使用Eclipse新建一个Java项目“JWSClient”将使用wsimport工具生成的类文件包复制到src目录下如下图所示 新建一个名为“com.guowei.ws.jwsclient”包新建一个类“JWSClientDemo”其代码如下 package com.guowei.ws.jwsclient;import com.guowei.ws.jws.HelloWorld;
import com.guowei.ws.jws.HelloWorld_Service;
import com.guowei.ws.jws.User;public class JWSClientDemo {public static void main(String[] args) {// TODO Auto-generated method stubHelloWorld_Service jwsService new HelloWorld_Service();HelloWorld hw jwsService.getHelloWorldImplPort();System.out.println(hw.sayHi(geek));User user new User();user.setName(Jobs);user.setDescription(apple);System.out.println(hw.sayHiToUser(user));}}运行程序结果如下说明成功调用Web Service。