【WebService】快速构建WebService示例
来源:互联网 发布:李刚疯狂java讲义 编辑:程序博客网 时间:2024/06/07 17:33
1 package com.slp.webservice; 2 3 import javax.jws.WebService; 4 5 /** 6 * Created by sanglp on 2017/2/25. 7 * 接口 8 */ 9 @WebService10 public interface IMyService {11 public int add(int a,int b);12 public int minus(int a,int b);13 }
package com.slp.webservice;import javax.jws.WebService;/** * Created by sanglp on 2017/2/25. * 实现 */@WebService(endpointInterface = "com.slp.webservice.IMyService")public class MyServiceImpl implements IMyService { @Override public int add(int a, int b) { System.out.println(a+"+"+b+"="+(a+b)); return a+b; } @Override public int minus(int a, int b) { System.out.println(a+"-"+b+"="+(a-b)); return a-b; }}
package com.slp.webservice;import javax.xml.ws.Endpoint;/** * Created by sanglp on 2017/2/25. * 服务 */public class MyServer { public static void main(String [] args){ String address="http://localhost:8888/ns"; Endpoint.publish(address,new MyServiceImpl()); }}
访问http://localhost:8888/ns?wsdl得到的页面
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" name="MyServiceImplService" targetNamespace="http://webservice.slp.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.slp.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><types><xsd:schema><xsd:import schemaLocation="http://localhost:8888/ns?xsd=1" namespace="http://webservice.slp.com/"/></xsd:schema></types><message name="add"><part name="parameters" element="tns:add"/></message><message name="addResponse"><part name="parameters" element="tns:addResponse"/></message><message name="minus"><part name="parameters" element="tns:minus"/></message><message name="minusResponse"><part name="parameters" element="tns:minusResponse"/></message><portType name="IMyService"><operation name="add"><input message="tns:add" wsam:Action="http://webservice.slp.com/IMyService/addRequest"/><output message="tns:addResponse" wsam:Action="http://webservice.slp.com/IMyService/addResponse"/></operation><operation name="minus"><input message="tns:minus" wsam:Action="http://webservice.slp.com/IMyService/minusRequest"/><output message="tns:minusResponse" wsam:Action="http://webservice.slp.com/IMyService/minusResponse"/></operation></portType><binding type="tns:IMyService" name="MyServiceImplPortBinding"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="add"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation><operation name="minus"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="MyServiceImplService"><port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding"><soap:address location="http://localhost:8888/ns"/></port></service></definitions>
package com.slp.webservice;import javax.xml.namespace.QName;import javax.xml.ws.Service;import java.net.MalformedURLException;import java.net.URL;/** * Created by sanglp on 2017/2/25. */public class TestClient { public static void main(String [] args) throws MalformedURLException { //创建访问wsdl服务地址的url URL url = new URL("http://localhost:8888/ns?wsdl"); //通过QName指明服务的具体消息 QName sname = new QName("http://webservice.slp.com/","MyServiceImplService"); //创建服务 Service service = Service.create(url,sname); //实现接口 IMyService ms=service.getPort(IMyService.class); System.out.println(ms.add(12,33)); //以上服务有问题,依然依赖于IMyService接口 }}
webService三要素:
SOAP,WSDL,UDDI.soap用来描述传递消息的格式,wsdl用来描述如何访问具体的接口,uudi用来管理,分发,查询webservice.
SOAP=RPC+HTTP+XML
SOAP简单的理解就是这样的一个开放协议:采用HTTP作为底层通讯协议
RPC作为一致性的调用途径,XML作为数据传送的格式,允许服务提供者和服务客户经过防火墙在INTERNET进行通讯交互。RPC的描叙可能不大准确,因为SOAP一开始构思就是要实现平台与环境的无关性和独立性,每一个通过网络的远程调用都可以通过SOAP封装起来,包括DCE(Distributed Computing Environment ) RPC CALLS,COM/DCOM CALLS, CORBA CALLS, JAVA CALLS,etc。
SOAP 使用 HTTP 传送 XML,尽管HTTP 不是有效率的通讯协议,而且 XML 还需要额外的文件解析(parse),两者使得交易的速度大大低于其它方案。但是XML 是一个开放、健全、有语义的讯息机制,而 HTTP 是一个广泛又能避免许多关于防火墙的问题,从而使SOAP得到了广泛的应用。但是如果效率对你来说很重要,那么你应该多考虑其它的方式,而不要用 SOAP。
wsdl讲解:
types:定义访问的类型
message:SOAP
portType:指明服务器的接口,并且通过operation绑定相应的消息,其中in表示参数,out表示返回值
binding:指定传递消息所用的格式
service:指定服务所发布的名称
- 【WebService】快速构建WebService示例
- webservice示例
- webservice 示例
- webService示例
- 五分钟快速用Flex构建WebService应用
- Restlet+Fastjson 快速构建轻量级 Java RESTful Webservice
- Flex构建WebService应用
- myeclipse构建webservice
- Axis框架构建webservice
- 采用CXF 构建webservice
- ant构建webservice客户端
- WebService 实例构建
- CXF构建webservice解析
- Axis2+Spring构建WebService
- 简单WebService示例
- Asp 调用WebService示例
- JS调用WebService示例
- spring+webservice工程示例
- js年月日时分秒的获取
- hdfs集群的HA
- 一个人的旅行 HDU
- Hadoop常用shell命令
- [JAVA基础]异常、断言、日志和调试
- 【WebService】快速构建WebService示例
- leetcode(310). Minimum Height Trees
- C#创建Windows服务与安装-图解
- HDU 6166 Senior Pan spfa(顶点集间最短路)
- 1864: [Zjoi2006]三色二叉树
- Python中布尔类型
- 8月23日小结
- 为什么不能通过返回值类型区分重载?
- 模板