WebService

时间:2022-04-29
本文章向大家介绍WebService,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Web Service概述:

    Web Service也叫XML Web Service。 WebService是一种轻量级的独立的通讯技术。是通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明。服务端提供服务供客户端调用, 具有夸平台跨语言的特性。

WSDL(Web Services Description Language):

    WSDL 文件是一个 XML 文档,webservice服务需要通过wsdl文件来说明自己有什么服务可以对外调用。并且有哪些方法、方法里面有哪些参数,  一般由程序自动生成。

    ①. 一个webservice对应唯一一个wsdl文档     ②. 定义webservice服务器端和客户端数据如何交换。

Soap (Simple Object Access Protocol)简单对象存取协议:

    是XML Web Service 的通信协议。webservice通过http协议发送和接收请求时, 发送的内容(请求报文)和接收的内容(响应报文)都是采用xml格式进行封装 , 这些特定的HTTP消息头和XML内容格式就是SOAP协议。

实例:

    服务端:

@WebService
public interface IProductRemote {
	@WebMethod
	String topup(String name);
}


//实现
@WebService
public class ProductImpl implements IProductRemote{

	@Override
	public String topup(String name) {
		return name + ",充值成功,请关注账户余额变更!";
	}

}

//发布
Endpoint.publish("http://127.0.0.1:8080/topup", new ProductImpl());
System.out.println("publish suc!");

//访问
http://127.0.0.1:8080/topup?wsdl

--------------------------------

通过wsimport -keep http://127.0.0.1:8080/topup?wsdl生成服务代码并引入client

客户端:

@WebService(name = "ProductImpl", targetNamespace = "http://impl.remote.ws.com/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface ProductImpl {


    /**
     * 
     * @param arg0
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "topup", targetNamespace = "http://impl.remote.ws.com/", className = "com.ws.remote.impl.Topup")
    @ResponseWrapper(localName = "topupResponse", targetNamespace = "http://impl.remote.ws.com/", className = "com.ws.remote.impl.TopupResponse")
    @Action(input = "http://impl.remote.ws.com/ProductImpl/topupRequest", output = "http://impl.remote.ws.com/ProductImpl/topupResponse")
    public String topup(
        @WebParam(name = "arg0", targetNamespace = "")
        String arg0);

}

//测试

ProductImplService service = new ProductImplService();
		ProductImpl remote = service.getProductImplPort();
		System.out.println(remote.topup("张三"));


控制台:
张三,充值成功,请关注账户余额变更!