分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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 * */@WebServicepublic interface HelloWorld { String sayHi(@WebParam(name="text") String text); String sayHiToUser(@WebParam(name="user") 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 stub System.out.println("sayHi called"); return "Hello " + text; } public String sayHiToUser(User user) { // TODO Auto-generated method stub System.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 stub System.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发布成功。