webService详解

一 服务器建立

1.1 创建接口

Web服务的接口通常称为SEI (Service Endpoint Interface)。

 

 

package org.zttc.service;

import javax.jws.WebService;

@WebService()
public interface IMyService {

 public int add(int a,int b);

 public int minus(int a,int b);

}

1.2 创建实现类

Web服务的实现类通常称为SIB (Service Implementation Bean)

 

package org.zttc.service;

import javax.jws.WebService;

//@WebService(endpointInterface="org.zttc.service.IMyService")// 用于JDK6.0以上的

@WebService(serviceName="MyServiceImpl")
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;
 }
}

 

1.3 创建服务

 

package org.zttc.service;

import javax.xml.ws.Endpoint;

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  可以查看wsdl文件

 

二 客户端的使用

 

package org.zttc.service;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class TestClient {
 public static void main(String[] args) {
  try {
   //创建访问wsdl服务地址的url
   URL url = new URL("http://localhost:8888/ns?wsdl");
   //通过Qname指明服务的具体信息
   QName sname = new QName("http://service.zttc.org/", "MyServiceImplService");
   //创建服务
   Service service = Service.create(url,sname);
   //实现接口
   IMyService ms = service.getPort(IMyService.class);
   System.out.println(ms.add(12,33));
   //以上服务有问题,依然依赖于IMyServie接口
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
 }
}

 

QName 的创建参数来自WSDL文件

<definitions targetNamespace="http://service.zttc.org/" name="MyServiceImplService">

三 wsimport的使用

cmd> wsimport -s 保存路径 http://localhost:9999/ns?wsdl

cmd> wsimport -d d:/webservice/01 -keep -verbose http://localhost:8888/ns?wsdl

-d            指定生成的目录

-keep       保持源文件

-verbose  显示生成的详细过程

-p            指定包名

客户端代码

 

package org.zttc.service;

import java.net.MalformedURLException;

public class TestClient2 {

 public static void main(String[] args) throws MalformedURLException {
  MyServiceImplService msis = new MyServiceImplService();
  IMyService ms = msis.getMyServiceImplPort();
  System.out.println(ms.minus(29,11));
 }

}

 

四 wsdl简单讲解

4.1 types

用来定义访问的类型,描述方法名、参数、返回值

http://localhost:8888/ns?xsd=1

4.2 message

SOAP(simple object access protocol)消息,一个方法一般对应两个,接收消息与发送消息。


4.3 portType

指明服务器的接口,并且通过operation绑定相应的in和out的消息:其中in表示参数,out表示返回值


4.4 binding

指定传递消息所使用的格式。早期会用soap encoding ,现在是literal


4.5 service

指定服务所发布的名称等基本信息

五 soap的使用和TCPMon

5.1 在eclipse中可以查看soap消息格式

J2EE视图->Launch the Web Services Explorer->WSDL网址

5.2 TCPMon

Listen Port #

客户端访问的接口,等于客户端首先将消息提交给TCPMon之后,再由TCPMon转发给服务器

Listener

   Target Hostname

   Target Port #

服务器的地址,TCPMon转发的地址

5.3 定义参数名称

 @WebResult(name="addResult")
 public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);
 
 @WebResult(name="minusResult")
 public int minus(@WebParam(name="a")int a,@WebParam(name="b")int b);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值