【WebService】快速构建WebService示例
package com.slp.webservice; import javax.jws.WebService; /**
* Created by sanglp on 2017/2/25.
* 接口
*/
@WebService
public interface IMyService {
public int add(int a,int b);
public int minus(int a,int b);
}
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示例的更多相关文章
- SpringBoot | 第三十四章:CXF构建WebService服务
前言 上一章节,讲解了如何使用Spring-WS构建WebService服务.其实,创建WebService的方式有很多的,今天来看看如何使用apache cxf来构建及调用WebService服务. ...
- 基于soapUI构建WebService测试框架
基于soapUI构建WebService测试框架 http://www.docin.com/p-775523285.html
- cxf构建webservice的两种方式
一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...
- (转)wsdl文件用SoapUI快速创建WebService,CXF生成客户端代码
原文地址:http://blog.csdn.net/fjekin/article/details/62234861 一.前言 最近项目接触到2C的很多接口,提供接口文档和WSDL文件,一开始测试接口都 ...
- webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)
1.定义接口 package org.WebService.ws.annotation; import javax.jws.WebService; @WebService public interfa ...
- 玩转Windows服务系列——使用Boost.Application快速构建Windows服务
玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...
- 【Android】如何快速构建Android Demo
[Android]如何快速构建Android Demo 简介 在 Android 学习的过程中,经常需要针对某些项目来写一些测试的例子,或者在做一些 demo 的时候,都需要先写 Activity 然 ...
- 快速构建ASP.NET MVC Admin主页
前言 后台开发人员一般不喜欢调样式,搞半天样式出不来,还要考虑各种浏览器兼容,费心费力不讨好,还好互联网时代有大量的资源共享,避免我们从零开始,现在就来看怎么快速构建一个ASP.NET MVC后台管理 ...
- 快速构建Windows 8风格应用15-ShareContract构建
原文:快速构建Windows 8风格应用15-ShareContract构建 本篇博文主要介绍共享数据包.如何构建共享源.如何构建共享目标.DataTransferManager类. 共享数据包 Da ...
随机推荐
- uboot中log处理
位图或logo和开机显示画面,是两个完全不同的东西. logo显示uboot相关信息,如版本号等. 开机画面是用户下载到固定位置后uboot加载的. 1.开机画面 在uboot中使用splash sc ...
- Hibernate- 基本查询
01.搭建开发环境 02.基本查询 package com.gordon.test; import java.text.DecimalFormat; import java.util.Arrays; ...
- form_tag
class SwitchesController < ApplicationController #before_filter :authenticate_user!, :except => ...
- Asp.Net MVC EasyUI DataGrid查询分页
function doSearch() { //查询方法 var searchValue = $('#txtQueryTC001').textbox('getText'); $('#dgCMSTC') ...
- JavaScript入门学习书籍的阶段选择
对于许多想学习 JavaScript 的朋友来说,无疑如何选择入门的书籍是他们最头疼的问题,或许也是他们一直畏惧,甚至放弃学习 JavaScript 的理由.在 JavaScript 方面,自己不是什 ...
- oracle权限详解
一.权限分类:系统权限:系统规定用户使用数据库的权限.(系统权限是对用户而言). 实体权限:某种权限用户对其它用户的表或视图的存取权限.(是针对表或视图而言的). 二.系统权限管理:1.系统权限分类: ...
- Android多线程任务的优化1:AsyncTask的缺陷 (转至 http://www.linuxidc.com/Linux/2011-09/43150.htm)
导语:在开发Android应用的过程中,我们需要时刻注意保障应用的稳定性和界面响应性,因为不稳定或者响应速度慢的应用将会给用户带来非常差的交互体验.在越来越讲究用户体验的大环境下,用户也许会因为应用的 ...
- openstack热迁移和冷迁移
转自: http://www.cnblogs.com/pycode/p/6494848.html 迁移类型: *非在线迁移 (有时也称之为‘迁移’).也就是在迁移到另外的计算节点时的这段时间虚拟机实例 ...
- C++primer中 CacheObj实现(非常有意思)
//CacheObj.h #ifndef __CacheObj__ #define __CacheObj__ #include <iostream> #include <stdexc ...
- liunx下误删除/var目录下的empty文件,导致ssh连接不上
清理Liunx上不用的文件,导致误删 /var/下的empty文件,因为用的是ftp删的,所以可能有隐藏文件没有看到,导致其他同事都登录不上去 解决方法: 1.在/var文件夹下,重新建立empty文 ...