WebService-01入门篇(简单实现)

一、准备工作(以下为本实例使用工具)

1、MyEclipse10.7.1

2、JDK 1.6.0_22

 

二、创建服务端

1、创建【Web Service Project】,命名为【TheService】。



 

 

 

2、创建【Class】类,命名为【ServiceHello】,位于【com.hyan.service】包下。



 

 

 

3、编写供客户端调用的方法,即编译方法代码。

    这里需要注意两点

    1.引包

    2.@WebService注释

package com.hyan.servie;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class ServiceHello {
	public String getValue(String name){
		return "我叫"+name;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String uri = "http://localhost:9001/Service/ServiceHello";
		Endpoint.publish(uri, new ServiceHello());
		System.out.println("service success:"+uri);
	}

}


4、进行编译

说明:编译失败的话,请将该项目引用的jdk设置为1.6.0_17版或更高版本



 

 

 

5、测试结果

测试地址:http://localhost:9001/Service/ServiceHello?wsdl

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 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://servie.hyan.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://servie.hyan.com/" name="ServiceHelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://servie.hyan.com/" schemaLocation="http://localhost:9001/Service/ServiceHello?xsd=1"/>
</xsd:schema>
</types>
<message name="getValue">
<part name="parameters" element="tns:getValue"/>
</message>
<message name="getValueResponse">
<part name="parameters" element="tns:getValueResponse"/>
</message>
<portType name="ServiceHello">
<operation name="getValue">
<input wsam:Action="http://servie.hyan.com/ServiceHello/getValueRequest" message="tns:getValue"/>
<output wsam:Action="http://servie.hyan.com/ServiceHello/getValueResponse" message="tns:getValueResponse"/>
</operation>
</portType>
<binding name="ServiceHelloPortBinding" type="tns:ServiceHello">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getValue">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="ServiceHelloService">
<port name="ServiceHelloPort" binding="tns:ServiceHelloPortBinding">
<soap:address location="http://localhost:9001/Service/ServiceHello"/>
</port>
</service>
</definitions>

三、生成客户端

1、创建【Web Service Project】,命名为【TheClient】。



 

 

 

 

2、命令提示窗口执行生成命令。

格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址”

示例:

wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello?wsdl

说明:

1)"src目录"地址不可含空格

2)“wsdl发布地址”不要漏了“?wsdl



 

 

3、刷新项目,检查生成类



 

 
 

 

四、最终测试

1、创建【Class】类,命名为【ServiceTest】,位于【com.hyan.test】包下。




package com.hyn.test;

import com.hyn.client.ServiceHello;
import com.hyn.client.ServiceHelloService;

public class ServiceTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ServiceHello helloPort = new ServiceHelloService().getServiceHelloPort();
		String value = helloPort.getValue(",Hyan");
		System.out.println(value);
	}

}

2、运行测试方法,并打印返回的结果。

五、注意事项

1、JDK版本过低问题

报类似如下的异常:runtime modeler error: Wrapper class com.hyan.service.jaxws.GetValue is not found. Have you run APT to generate them?



 

原因:JDK版本低于1.6.0_17

解决方法:调整该服务端项目所引用的JDK版本为安装的高版本JDK



 

 

 

 

 

 

 

 

 

 

 

 

2、生成命令路径含空格问题

报类似如下的异常:directory not found: G:\SVN_FILE\GB\05



 

原因:客户端src路径不可含有空格

解决方法:在不含空格路径的文件夹下重新创建客户端即可。

 

3、生成命令不完整问题

报类似如下的异常:[ERROR] failed.noservice=Could not find wsdl:service in the provided WSDL(s):



 

原因:生成命令末尾缺少“?wsdl”

解决方法:补上即可。

错误不完整的命令示例:

wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello

正确完整的命令示例:

wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello?wsdl



转载于:https://my.oschina.net/chaon/blog/647200

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值