最近项目中有用到WebService,因而就研究了一下,可是关于intellij 开发 WebService 的文章极少,要不就是多年之前,因而研究一下,写这篇博文。纯属记录,分享,中间有不对的地方,望请指正,下面开始。java
首先,开发WebService的服务器端,不须要借助任何的其余,JDK就能够搞定,只要在类上标注了@WebService,以及在方法上,标注了@WebMethod方法,就能够认为他是一个WebService。服务器
下面,先显示一下个人目录结构:网络
在server包下的是服务器端代码,在client包下的是客户端代码。ide
下面看一下,服务端代码:url
HelloWorldWS.javaspa
1 package server; 2 3 /** 4 * Created by Lin_Yang on 2014/12/16. 5 */ 6 public interface HelloWorldWS { 7 public String sayHello(String name); 8 }
这是一个接口。(固然也能够没有这个接口,效果是同样的).net
HelloWorldImpl.java3d
package server; import javax.jws.WebMethod; import javax.jws.WebService; /** * Created by Lin_Yang on 2014/12/16. */ @WebService public class HelloWorldImpl implements HelloWorldWS { @WebMethod @Override public String sayHello(String name) { String str="欢迎你:"+name; System.out.println(str); return str; } }
注意上面的两个注释@WebService 和 @WebMethodcode
下面就能够发布这个WebService了server
Publish.java
package server; import javax.xml.ws.Endpoint; /** * Created by Lin_Yang on 2014/12/16. */ public class Publish { public static void main(String args[]){ Object implementor = new HelloWorldImpl(); String address = "http://localhost:8989/HelloWorld"; //发布到的地址 Endpoint.publish(address, implementor); System.out.println("发布成功"); } }
客户端的代码很简单,这里就不连篇累牍了。
下面着重说一下客户端代码的建立过程。
intellij14 中内置了WebService 的客户端代码的实现方式,他是使用的 JAX-WS.废话很少说,上图。
在Intellj 的 Tool-->WebServices-->Generate Java Code From WSDL (一看就是根据WSDL文档生成java代码了)
随后应该弹出这个一个提示框。
首先,Web service wsdl url 是指明WSDL文档的位置,这里的地址和服务端发布的地址相对应。他也能够不从网络中寻找这个WSDL文档,也能够从本地寻找。
格式是这样的:file:/c:/CRMLOYMemberCreateWorkflow.wsdl 指定文档的地址。
按照上图的配置,就会在client包中生成这些代码
下面咱们就能够根据这些生成的代码,访问服务端的WebService了
test/client.java
package client.test; import client.HelloWorldImpl; import client.HelloWorldImplService; /** * Created by Lin_Yang on 2014/12/16. */ public class Client { public static void main(String args[]){ HelloWorldImplService helloWorldImplService=new HelloWorldImplService(); HelloWorldImpl helloWorld= helloWorldImplService.getHelloWorldImplPort(); String returnStr= helloWorld.sayHello("先知后觉"); System.out.println(returnStr); } }
服务端显示
客户端显示:
但愿能够给你们一些启示。