WebService简单入门

article/2025/10/12 20:42:47

1. JAX-WS发布WebService

创建web工程
创建simple包,和server、client两个子包。正常情况下server和client应该是两个项目,这里我们只是演示效果,所以简化写到一个项目中:

1.1 创建服务类Server

package simple.server;import javax.jws.WebService;
import javax.xml.ws.Endpoint;//这里要加上WebService注解
@WebService
public class SimpleServer {//要发布出去的方法public String sayHello() {return "hello world";}//要发布出去的方法public String speak(@WebParam(name = "word") String word) {return word + ":webservice";}//使用main方法发布出去public static void main(String[] args) {//第一个参数是地址,localhost是本机,//9001是端口,端口可以是任意一个未占用的端口//SimpleService是自己起的服务名,任意//第二个参数是要发布的这个类的对象Endpoint.publish("http://localhost:9001/SimpleService", new SimpleServer());System.out.println("Publish Success~");//看到这个输出代表发布成功了}
}

运行main方法后在浏览器中输入
http://localhost:9001/SimpleService?wsdl
可以看到服务信息:
在这里插入图片描述

Wsdl文档从下往上读
Types - 数据类型定义的容器,它使用某种类型系统(一般地使用XML
Schema中的类型系统)。(入参和出参的数据类型)
Message -通信消息的数据结构的抽象类型化定义。使用Types所定义的类型来定义整个消息的数据结构(入参和出参)。
Operation - 对服务中所支持的操作的抽象描述,一般单个Operation描述了一个访问入口的请求/响应消息对(方法)。
PortType -对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持(服务类)。
Binding - 特定服务访问点与具体服务类的绑定(不看内容,看关系)。 Port - 定义为webservice单个服务访问点。
Service-相关服务访问点的集合。
访问上面的schemaLocation="http://localhost:9001/SimpleService?xsd=1"网址,可以看到具体方法的描述信息
在这里插入图片描述

如果要使用web方式发布这个webservice,只需要写一个servlet,并在tomcat启动时就加载这个servlet,在servlet的int方法中发布webservice。
如:

package simple.server;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.ws.Endpoint;public class PublishServlet extends HttpServlet{@Overridepublic void init(ServletConfig servletConfig) throws ServletException {super.init(servletConfig);//发布webserviceEndpoint.publish("http://localhost:9001/SimpleService", new SimpleServer());System.out.println("Publish Success~");//看到这个输出代表发布成功了}
}

web.xml中配置:

<servlet><servlet-name>PublishServlet</servlet-name><servlet-class>simple.server.PublishServlet</servlet-class><load-on-startup>1</load-on-startup><!--启动就加载-->
</servlet>
<servlet-mapping><servlet-name>PublishServlet</servlet-name><url-pattern>/servlet/publish</url-pattern>
</servlet-mapping>

需要servlet的jar包

<!--servlet依赖jar包-->
<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version>
</dependency>

1.2 创建客户端

使用jdk自带命令调用WebService

在这里插入图片描述

请求webservice会在本地生成类
wsimport 是请求webservice
-encoding utf-8 指定生成的java文件编码格式为utf-8
-s 后面是文件存放的工程路径
-p 是生成的java文件存放的包名
-keep 后面接的是1.1中发布出去的服务地址
运行成功后,工程中会多出几个类:
在这里插入图片描述

创建测试客户端类MySimpleClient

package simple.client;import org.junit.Test;import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;public class MySimpleClient {@Testpublic void testJdkMethod() {//<service name="SimpleServerService">//   <port name="SimpleServerPort" binding="tns:SimpleServerPortBinding">//     <soap:address location="http://localhost:9001/SimpleService"/>//   </port>// </service>//这个是xml文件中的service-name// <service name="SimpleServerService">SimpleServerService simpleServerService = new SimpleServerService();//这个是<port name="SimpleServerPort"SimpleServer simpleServer = simpleServerService.getSimpleServerPort();System.out.println(simpleServer.sayHello());}
}

通过jdk生成的SimpleServer,可以调用相应的方法,实际上返回响应的是服务器,但执行的时候就像调用自己写的类一样。可以清楚的看到方法和参数。
另一种调用的方式,直接使用java方法,不生成类:
新建一个other包,存放如下代码:

package simple.other;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;//对应xml文件
//<definitions targetNamespace="http://server.simple/" name="SimpleServerService">
@WebService(name = "SimpleServerService", targetNamespace = "http://server.simple/")
@XmlSeeAlso({})
public interface MySimpleClient {@WebMethod@RequestWrapper(localName = "sayHello")@ResponseWrapper(localName = "sayHelloResponse")public String sayHello();@WebMethod@RequestWrapper(localName = "speak")@ResponseWrapper(localName = "speakResponse")public String speak(@WebParam(name = "word")String word);
}

测试代码:

package simple.other;import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;public class OtherTest {public static void main(String[] args) throws Exception {URL wsdlUrl = new URL("http://localhost:9001/SimpleService?wsdl");// targetNamespace="http://server.simple/" name="SimpleServerService"Service s = Service.create(wsdlUrl,new QName("http://server.simple/","SimpleServerService"));MySimpleClient client = s.getPort(new QName("http://server.simple/","SimpleServerPort"), MySimpleClient.class);System.out.println(client.sayHello());System.out.println(client.speak("123"));}
}

2. cxf发布WebService

JAX-WS是一种规范,CXF是他的实现。CXF可以不必关心服务端的实现方式。
为了简化代码,我们把服务端和客户端写在一个工程里,正常应该写在两个工程

2.1 发布服务

新建web工程,导入jar包:

<dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.2.0</version></dependency><dependency><groupId>org.apache.cxf.karaf</groupId><artifactId>apache-cxf</artifactId><version>3.2.0</version></dependency><!--日志文件--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version></dependency>
</dependencies>

CXF发布服务需要一个接口和一个实现类:

package server;import javax.jws.WebParam;
import javax.jws.WebService;@WebService(name = "CXF", targetNamespace = "http://server.cxf/")
public interface CxfServer {String sayHello();String speak(@WebParam(name = "word") String world);
}

实现类:

package server;public class CxfServerImpl implements CxfServer {@Overridepublic String sayHello() {return "Hello CXF";}@Overridepublic String speak(String word) {return word + "CXF";}
}

发布服务:

package server;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;public class CXFServerTest {public static void main(String[] args) {// 创建JaxWsServerFactoryBean对象JaxWsServerFactoryBean serverFactoryBean = new JaxWsServerFactoryBean();// 设置服务端地址serverFactoryBean.setAddress("http://127.0.0.1:9999/cxf");// 设置服务接口serverFactoryBean.setServiceClass(CxfServer.class);// 设置实现类对象serverFactoryBean.setServiceBean(new CxfServerImpl());// 发布服务serverFactoryBean.create();System.out.println("发布成功");}
}

浏览器中访问:http://127.0.0.1:9999/cxf?wsdl
在这里插入图片描述

2.2 调用服务

package client;import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public class CxfClientTest {public static void main(String[] args) throws Exception {JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();Client client = clientFactory.createClient("http://127.0.0.1:9999/cxf?wsdl");//直接调用方法,不用关心服务端是怎么实现的Object[] result = client.invoke("sayHello");System.out.println(result[0]);Object[] result2 = client.invoke("speak", "123");System.out.println(result2[0]);}
}

2.3 Spring与CXF集成

引入spring的jar

<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.11.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.11.RELEASE</version>
</dependency>

spring-cxf.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--发布服务implementor是接口实现类,address在访问的时候加载路径里--><jaxws:endpoint id="cxfDemo" implementor="server.CxfServerImpl" address="/cxf"/>
</beans>

web.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-cxf.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

使用spring发布的时候,在接口实现类上加上注解,保证发布出去的targetNamespace一致:

@WebService(name = "CXF", targetNamespace = "http://server.cxf/")
public class CxfServerImpl implements CxfServer {

浏览器中访问:http://127.0.0.1:8080/services/cxf?wsdl
在这里插入图片描述

测试方法与2.2中相同,更换访问地址即可。


http://chatgpt.dhexx.cn/article/rBZdx1bF.shtml

相关文章

WebService教程实例

一、准备工作 1、Myeclipse 2014 2、jdk8.0 二、创建服务端 1、创建【Web Service Project】&#xff0c;命名为【TheService】。 2、创建【Class】类&#xff0c;命名为【ServiceHello】&#xff0c;位于【com.hyan.service】包下。 3、编写供客户端调用的方法&#xff0c;即编…

Java小白翻身 - webservice教程2

来一个HelloWorld&#xff0c;SpringBoot发布WebService可简单啦。 1、搭建项目2、配置pom.xml3、建services服务包4、登陆接口类5、登陆接口实现类6、创建CXF配置类7、Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决8、访问webservice9、…

idea2021创建webservice教程

一、创建服务端 1、创建一个新的java工程 2、工程项目右键添加框架依赖 3、选择webservices 4、点击确定完成创建。创建后项目会添加相关依赖。以及测试服务类。 5、代码介绍 // 添加WebService注解 WebService() public class HelloWorld {// 方法添加WebMethod注解W…

WebService教程

最近在学习WebService&#xff0c;今天尝试用Eclipse的插件生成JAX-WS WebService&#xff0c;结果遇到了不少的问题啊&#xff0c;调试了大半天终于把程序跑通了。现在把步骤和问题记录一下&#xff0c;也为了以后遇到相同的问题时能够及时解决。首先利用Eclipse生成WebServic…

WebService入门教程(服务端发布WebService)

本篇内容过多&#xff0c;时间紧迫的朋友可以通过目录快速筛选自己想要看的内容&#xff0c;本人接触webservice也没多久&#xff0c;也处于学习阶段&#xff0c;如果有错误请指正&#xff0c;如果已经是大神请略过这篇文章&#xff0c;这篇文章不涉及webservice的底层原理&…

WebService最详细教程

WebService相关概念 基础概念 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,就是说服务端程序采用java编写&#xff0c;客户端程序则可以采用其他编程语言编写&#xff0c;反之亦然&#xff01;跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统…

webSerivce简明使用教程

茫茫人海千千万万&#xff0c;感谢这一秒你看到这里。希望我的文章对你的有所帮助&#xff01; 愿你在未来的日子&#xff0c;保持热爱&#xff0c;奔赴山海&#xff01; &#x1f440; 前言 因为前段时间&#xff0c;需要使用到webService来调用公司的其他系统api接口&#x…

WebService详细讲解

1 学习目标2 webservice 基本概念 2.1 什么是web服务2.2 简介2.3 术语 2.3.1 webservice开发规范2.3.2 SOAP 协议2.3.3 wsdl说明书2.3.4 UDDI2.4 应用场景2.5 优缺点 2.5.1 优点&#xff1a;2.5.2 缺点&#xff1a;2.6 面向服务架构SOA3 ApacheCXF 框架介绍 3.1…

11步教你入门webservice

新建立一个javaWeb项目&#xff0c;一个java类&#xff0c;如图&#xff1a; 1.具体很简单 首先要创建一个web工程 2、接下来我们就要将项目中的TestService的这个类生成WebService服务端&#xff0c;选择new Web Service&#xff0c;如图&#xff1a; 3 4.Next,选择jav…

php 实现分页功能(class封装了功能)

前言 分页是一个很常见的功能&#xff0c;我这里提供了分类类(class)&#xff0c;用于前端页面中的四个按钮&#xff1a; 首页下一页上一页尾页 上面的演示非常不直观&#xff0c;但足可以证明这个类可以完成分页功能。 完整的代码 附有非常详细的注释&#xff0c;但需要有…

PHP分页技术详解

直接上代码&#xff0c;注释很详细了。 <?php /** * php分页技术详解 * author jahng */header("Content-type: text/html; charsetUTF-8");echo<link rel"stylesheet" type"text/css" href"css.css" media"all" /&g…

PHP的分页处理

HTML页面&#xff1a; <div class"page"> {$data->render()|raw} </div> 控制器页面&#xff1a; $data ArticleInfo:: where(cate_id, $this->cid)->whereLike(title,%.$get.%)->paginate([ query>[cid>$this->cid], l…

php分页

这是学生信息管理每三条一页> <?phpfunction news($pageNum 1, $pageSize 3){$array array();$mysqlnew mysql();$rs "select * from user limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;$r $mysql->query($rs);while ($obj…

PHP-分页

1.6 分页 1.6.1 分析 -- 1、获取当前页码的数据 页码 SQL语句 1 select * from products limit 0,10 2 select * from products limit 10,10 3 select * from products limit 20,10 结论&#xff1a; $pageno&#xff1a;页码 $startno:起始位置 $pagesize10:页面大小…

PHP分页查询

1、创建Page.php类&#xff0c;代码如下&#xff1a; <?php /*** 分页模板类*/ class Page {private $cur_page;//当前页private $total;//总条数private $page_size 10;//每页显示的条数private $total_page;//总页数private $first_page;//首页显示名称private $pre_pa…

全功能PHP分页条

网上可以找到的ASP、PHP分页条很多。我也不能免俗&#xff0c;发表一个献献丑。唯一聊以自慰的是这个分页条能生成的显示样式还是很多的&#xff0c;相信能满足大部分人的需要。另一个特点就是使用特别简单&#xff0c;一般传递两个参数即可使用。文档里有使用样例和效果图。发…

php原生分页

自己写一个原生php分页&#xff1a; <?php $linkMySQL_connect(localhost,用户名,密码); mysql_select_db(数据库名称,$link); mysql_query(set names utf8); $resultmysql_query("select * from 表名"); $count mysql_num_rows($result); $Page_size10; …

如何用php实现分页效果

分页效果在网页中是常见的,可是怎样才能实现分页呢,今天做了两种方法来实现一下分页的效果 首先,我们需要准备在数据库里面准备一个表,并且插入数据,这些都是必需的前提工作了,不多说,如图所示(库名为jereh,表名为n_content): 步骤分析: 我们需要分页的话,需要用…

超级好用的PHP分页类

<?phpclass Page {private $total; //总记录private $pagesize; //每页显示多少条private $limit; //limitprivate $page; //当前页码private $pagenum; //总页码private $url; //地址private $bothnum; //两边保持数字分页的量//构造方法初始化pub…

PHP-分页具体实现及代码

数据分页概述 对大量数据进行分页显示是 Web 开发中最常见的情况&#xff0c;但大多刚开始接触 Web 开发的开发人员&#xff0c;对分页技术往往比较迷惘&#xff0c;本节教程以一个分页显示留言板的数据为例就来演示一下 PHP 中基本的数据分页显示原理。 本节教程需要用到的 …