分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

WebService简单使用

发布时间:2023-09-06 02:02责任编辑:沈小雨关键词:Web

一.WebService发布服务方式

  方式一:使用jre自己提供的方式 jax-ws

    1)在类上或者相应类的接口或者抽象类上添加注解@WebService

    2)发布服务 Endpoint.publish("http://127.0.0.1:8800/hello", new HelloServiceImpl());

    3) http://127.0.0.1:8800/hello?wsdl 可以正常显示说明发布服务成功

  方式二:使用CXF的方式发布服务

    1)需要引入相应的jar包

 ???????<dependency> ???????????<groupId>org.apache.cxf</groupId> ???????????<artifactId>cxf-rt-frontend-jaxws</artifactId> ???????????<version>2.2.3</version> ???????</dependency> ???????<dependency> ???????????<groupId>org.apache.cxf</groupId> ???????????<artifactId>cxf-rt-transports-http</artifactId> ???????????<version>2.2.3</version> ???????</dependency> ???????<dependency> ???????????<groupId>org.apache.cxf</groupId> ???????????<artifactId>cxf-rt-transports-http-jetty</artifactId> ???????????<version>2.2.3</version> ???????</dependency>
CXF发布服务需要引入的jar

    2)在类上或者相应类的接口或者抽象类上添加注解@WebService

    3)发布服务 Endpoint.publish("http://127.0.0.1:8800/hello", new HelloServiceImpl());

    4) http://127.0.0.1:8800/hello?wsdl 可以正常显示说明发布服务成功 和 方式一 生成的wsdl文档不太一样了

二.WebService调用服务方式

  方式一:使用jre自己提供的方式 jax-ws 可以针对任何情况

    a.选择新建 Web Service Client

    

    b.输入wsdl地址并点击 Finish

    c.调用生成的代码

      1.使用代理的方式

 ???????WSServerProxy wsServerProxy = new WSServerProxy(); ???????String execBusinessMethod = wsServerProxy.execBusinessMethod("nice", "123213"); ???????System.out.println(execBusinessMethod); ???
代理方式调用服务端

      2.使用接口方式

 ??????//注意接口要和服务端一致的注解@WebService@WebParam.... ????????URL url = new URL("http://127.0.0.1:8899/zl?wsdl"); //服务的wsdl文档地址 ???????QName qname=new QName("http://zl.com/","WSServerService"); //wsdl上的targetNamespace,和后面的name的值 ???????Service service=Service.create(url, qname); ???????WSServer ms=service.getPort(WSServer.class); ???????String hello = ms.execBusinessMethod("good", "123123"); ???????System.out.println(hello); ???
接口方式调用服务端

  方式二:使用axis方式调用 只能是CXF的提供服务的方式

    a.需要引用的jar

<!-- 引用的jar包 --><!-- https://mvnrepository.com/artifact/axis/axis --> ???<dependency> ???????<groupId>axis</groupId> ???????<artifactId>axis</artifactId> ???????<version>1.4</version> ???</dependency> ???<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api --> ???<dependency> ???????<groupId>javax.mail</groupId> ???????<artifactId>javax.mail-api</artifactId> ???????<version>1.5.5</version> ???</dependency> ???<!-- https://mvnrepository.com/artifact/javax.activation/activation --> ???<dependency> ???????<groupId>javax.activation</groupId> ???????<artifactId>activation</artifactId> ???????<version>1.1</version> ???</dependency>
axis调用服务要引用的jar

    b.调用服务代码 注意包不要引错

import java.net.MalformedURLException;import java.net.URL;import java.rmi.RemoteException;import javax.xml.namespace.QName;import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;import org.apache.axis.client.Service; ???????//new 一个服务 ???????Service sv = new Service(); ?????????//创建一个call对象 ???????Call call = (Call) sv.createCall(); ?????????//设置要调用的接口地址 ???????call.setTargetEndpointAddress(new URL("http://127.0.0.1:8899/hello")); ????????//设置要调用的接口方法 ????????call.setOperationName(new QName("getHello")); ?????????//设置参数 第二个参数表示String类型,第三个参数表示入参 ???????call.addParameter("str", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); ???????//返回参数类型 ???????call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); ???????//开始调用方法并返回相应数据信息,以xml格式的字符串返回,也可以json格式主要看对方用什么方式返回 ???????Object result = ?call.invoke(new Object[]{"nice"}); ???????System.out.println(result);//打印字符串
调用服务代码

   

    

    

WebService简单使用

原文地址:https://www.cnblogs.com/codeLei/p/9261126.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved