webservice开发入门

自己开发个webservic小Demo


第一步:写webservice服务,即创建服务端

本例子非常简单,提供的服务非常简单:你输入姓名,返回"xxx,你吃了吗?"


好了进入编码:

服务端:

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class HelloWS {
	
	@WebMethod(exclude=false)
	public void say(String name){
		System.out.println(name+",你吃了吗?");
	}
	
	public static void main(String[] args) {
		
		Endpoint.publish("http://192.168.1.10:8899/sayYou", new HelloWS());
	}

}
需要注意的地方:

1、注解@WebService

2、注解@WebMethod(exclude=false),当为false为服务向外提供,当为true时,服务不提供

3、类Endpoint的publish()方法,第一个参数为发布地址、第二个参数为服务类对象


至此服务端编写完成。只需运行main方法后,服务就发布了!

这样你就可以访问了:

先测试一下:

在浏览器中输入:http://192.168.1.10:8899/sayYou?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.1.6 in JDK 6. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.zmm.mall.hyzy.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.zmm.mall.hyzy.com/" name="HelloWSService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.zmm.mall.hyzy.com/" schemaLocation="http://192.168.1.10:8899/sayYou?xsd=1"/>
</xsd:schema>
</types>
<message name="say">
<part name="parameters" element="tns:say"/>
</message>
<message name="sayResponse">
<part name="parameters" element="tns:sayResponse"/>
</message>
<portType name="HelloWS">
<operation name="say">
<input message="tns:say"/>
<output message="tns:sayResponse"/>
</operation>
</portType>
<binding name="HelloWSPortBinding" type="tns:HelloWS">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="say">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloWSService">
<port name="HelloWSPort" binding="tns:HelloWSPortBinding">
<soap:address location="http://192.168.1.10:8899/sayYou"/>
</port>
</service>
</definitions>


如果看到上面的WSDL文件说明就离成功就迈进一步了。


第二步:获得webservice的java源码类和字节码文件

通过dos命令来完成:wsimport -s . -p com.hyzy.mall.zmm.wsClient http://192.168.1.10:8899/sayYou?wsdl

说明:

  -s  表示生产源代码和字节码
           .   表示存放在当前目录下
           -p   表示为生成的源代码设置一个包名,提倡用客户端包名,
           http://192.168.1.10:8899/sayYou?wsdl   表示第三方网站webservice服务的WSDL文件


然后你就在指定的文件夹下就可看到很多文件了,拷贝其中的java类至你的工程中:

编写个测试类:

public class TestHellWS {
	
	public static void main(String[] args) {
		HelloWSService helloWSService=new HelloWSService();
		HelloWS helloWS = helloWSService.getHelloWSPort();
		
		helloWS.say("乐嘉");
		
	}

}

运行main函数就能看到结果了。


谢谢!






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值