学习网址: JAVA6开发WebService (一)
这篇博客枯燥言语少,直接一个可运行的代码,先动手一试,我习惯的学习方式是“知其然再知其所以然”,先会用再追其原理
模仿实例,遇见问题记录
1、JDK安装及验证 JDK版本1.6u17及以上,我的是 :java version "1.7.0_02" ,符合
2、eclipse建立java项目,项目名称WebServiceTest
Hello代码如下:
[java] view plain copy print ?
- package com.rwq.jdkwebservice;
- import javax.jws.WebService;
- import javax.xml.ws.Endpoint;
- @WebService
- public class Hello {
- public String sayHello(String name) {
- return "Hello " + name;
- }
- public static void main(String[] args){
- Endpoint.publish("http://localhost:8080/com.rwq.jdkwebservice.Hello", new Hello());
- System.out.println("Success");
- }
- }
3、运行Hello,控制台打印出Success ,即为成功
注意:执行是咧过程中Hello必须处于运行状态,不可关闭
4、打开浏览器输入:http://localhost:8080/com.rwq.jdkwebservice.Hello 可以看见如下Web Services信息
5、打开浏览器输入:http://localhost:8080/com.rwq.jdkwebservice.Hello?wsdl会看到WSDL信息
6、打开命令行,转到WebServiceTest项目src所在路径,执行:wsimport -p com.rwq.client -keep http://localhost:8080/com.rwq.jdkwebservice.Hello?wsdl 【 wsimport -p [包名] -keep [发布的服务地址?wsdl] 用以生成客户端代码】
注意:此时的Hello处于运行状态,不可关闭。若关闭报如下错误:
[plain] view plain copy print ?
- [ERROR] Connection refused: connect
- Failed to read the WSDL document: http://localhost:8080/com.rwq.jdkwebservice.He
- llo?wsdl, because 1) could not find the document; /2) the document could not be
- read; 3) the root element of the document is not <wsdl:definitions>.
- [ERROR] failed.noservice=Could not find wsdl:service in the provided WSDL(s):
- At least one WSDL with at least one service definition needs to be provided.
- Failed to parse the WSDL.
7、eclipse刷新WebServiceTest项目
自动生成了客户端代码
8、写个示例程序调用一下
[java] view plain copy print ?
- import com.rwq.client.HelloService;
- import com.rwq.client.Hello;
- public class HelloClient {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Hello hello = new HelloService().getHelloPort();
- String s = hello.sayHello("why");
- System.out.println(s);
- s = hello.sayHello("RWQ");
- System.out.println(s);
- }
- }
Hello why
Hello RWQ
注意:此时的Hello处于运行状态,不可关闭。若关闭报如下错误:
[plain] view plain copy print ?
- at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:992)
- at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:928)
- at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:846)
- at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1296)
- at java.net.URL.openStream(URL.java:1035)
至此,第一个java的webService示例正确运行完成
心中疑问:
1、若我的其他项目想调用这个interface,怎么做?
2、若其他地址想调用我这个interface,怎么做?
答疑解惑继续学