加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webService详解

发布时间:2020-12-16 23:52:53 所属栏目:安全 来源:网络整理
导读:一 服务器建立 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

一 服务器建立

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读