webservice笔记

1,简介

webservice,是一种跨编程语言和跨操作系统平台的远程调用技术。
webservice三要素:soap、wsdl、uddi

2,服务端
2.1创建项目
在这里插入图片描述
2.2 编写服务类,并发布服务

import com.test.service.impl.HelloServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;public class Server {public static void main(String[] args) {//发布服务的工厂JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();//设置服务地址factory.setAddress("http://localhost:8000/ws/hello");//设置服务类factory.setServiceBean(new HelloServiceImpl());//添加日志输入、输出拦截器,观察soap请求、响应内容factory.getInInterceptors().add(new LoggingInInterceptor());factory.getOutInterceptors().add(new LoggingOutInterceptor());//发布服务factory.create();System.out.println("服务发布成功--");}
}# 查看wsdl说明书
http://localhost:8000/ws/hello?wsdl

3,客户端
3.1 客户端需有服务端接口
在这里插入图片描述
3.2 客户端调用服务

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class Client {public static void main(String[] args) {//服务接口访问地址String address="http://localhost:8000/ws/hello";//创建cxf代理工厂JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();//设置远程访问服务端地址factory.setAddress(address);//设置接口类型factory.setServiceClass(HelloService.class);//对接口生成代理对象HelloService helloService=factory.create(HelloService.class);//代理对象System.out.println(helloService.getClass());//远程访问服务端方法String content = helloService.getName("张三");System.out.println(content);}
}

4,web发布jaxws服务
4.1 创建项目
在这里插入图片描述
4.2 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.eclipse.maven</groupId><artifactId>server_jaxws_spring</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><!-- 要进行jaxws服务开发 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><showWarnings>true</showWarnings></configuration></plugin></plugins></pluginManagement></build>
</project>

4.3 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- cxfservlet配置 --><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>/ws/*</url-pattern></servlet-mapping><!-- spring配置 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

4.4 applicationContext.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"xmlns:cxf="http://cxf.apache.org/core"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"><!-- spring发布服务 --><jaxws:server address="/hello"><jaxws:serviceBean><bean class="com.test.service.impl.HelloServiceImpl"></bean></jaxws:serviceBean></jaxws:server>
</beans>

5,服务端调用
5.1,创建项目
在这里插入图片描述
5.2 ,applicationContext.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"xmlns:cxf="http://cxf.apache.org/core"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"><!-- spring发布服务 --><jaxws:client id="helloService" serviceClass="com.test.service.HelloService" address="http://localhost:8080/server_jaxws_spring/ws/hello"></jaxws:client>
</beans>

5.3,测试类Client

import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {//注入对象@Resourceprivate HelloService helloService;@Testpublic void test() {System.out.println(helloService.getClass());System.out.println(helloService.getName("李四"));}}

6,jaxrs规范服务
6.1 创建项目
在这里插入图片描述
6.2 pom.xml配置

<dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-client</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-extension-providers</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.codehaus.jettison</groupId><artifactId>jettison</artifactId><version>1.3.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><showWarnings>true</showWarnings></configuration></plugin></plugins></pluginManagement></build>

6.3 web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- cxfservlet配置 --><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>/ws/*</url-pattern></servlet-mapping><!-- spring配置 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

6.4 User类

import javax.xml.bind.annotation.XmlRootElement;/*** XmlRootElement指定对象序列化为xml或json数据时的根节点*/
@XmlRootElement(name="User")
public class User {private Integer id;private String username;private String sex;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public User() {super();}public User(Integer id, String username, String sex) {super();this.id = id;this.username = username;this.sex = sex;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", sex=" + sex + "]";}}

6.5 UserService 接口

import java.util.List;import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;import com.test.entity.User;@Path("/userService")
@Produces("*/*")
public interface UserService {//表示处理请求的类型@POST@Path("/user")//服务器请求的数据格式类型@Consumes({"application/xml","application/json"})public void saveUser(User user);@PUT@Path("/user/{id}")@Consumes({"application/xml","application/json"})public void updateUser(@PathParam("id")Integer id);@GET@Path("/user/{id}")//服务器支持返回数据的格式@Produces({"application/xml","application/json"})public User findUserById(@PathParam("id")Integer id);@DELETE@Path("/user/{id}")@Consumes({"application/xml","application/json"})public void deleteUser(@PathParam("id")Integer id);
}

6.6 applicationContext.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"xmlns:cxf="http://cxf.apache.org/core"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"><!-- spring发布服务 --><jaxrs:server address="/userService"><jaxrs:serviceBeans><bean class="com.test.service.impl.UserServiceImpl"></bean></jaxrs:serviceBeans></jaxrs:server>
</beans>

7 jaxrs规范,客户端使用
7.1 创建项目
在这里插入图片描述
7.2 Client类

@Test
public void testSaveUser() {User user = new User(3, "王五", "女");//type指定请求的数据格式WebClient.create("http://localhost:8080/server_jaxrs_spring/ws/userService/userService/user").type(MediaType.APPLICATION_JSON).post(user);
}
@Test
public void testFindUserById() {User user = WebClient.create("http://localhost:8080/server_jaxrs_spring/ws/userService/userService/user/2").type(MediaType.APPLICATION_JSON).get(User.class);System.out.println(user);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/194725.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gitlab环境准备

1.准备环境 gitlab只支持linux系统&#xff0c;本人在虚拟机下使用Ubuntu作为操作系统&#xff0c;gitlab镜像要使用和操作系统版本对应的版本&#xff0c;(ubuntu18.04,gitlab-ce_13.2.3-ce.0_amd64 .deb) book100ask:/$ lsb_release -a No LSB modules are available. Dist…

java--贪吃蛇

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random;public class Snake extends JFrame implements KeyListener, ActionListener, MouseListener {int slong 2;//蛇当前长度//蛇坐标int[] Snakex new int[100];int[] Snakey new…

如何使用贝锐花生壳内网穿透远程访问JupyterNotebook?

在数据科学领域&#xff0c;Jupyter Notebook 已成为处理数据的必备工具。 其用途包括数据清理和探索、可视化、机器学习和大数据分析。Jupyter Notebook的安装非常简单&#xff0c;如果你是小白&#xff0c;那么建议你通过安装Anaconda来解决Jupyter Notebook的安装问题&#…

贝锐蒲公英路由器X4C如何远程访问NAS?

在目前网盘前路坎坷的情况下&#xff0c;私人云盘已然是一种新的趋势&#xff01;那自己打造一个私有云盘&#xff0c;是否需要高成本或是高门槛呢&#xff1f;其实并不用&#xff01;蒲公英针对个人玩家打造了全方位的私有云解决方案。 &#xff08;1&#xff09;入门级玩家只…

Unity 场景烘培 ——unity Post-Processing后处理1(四)

提示&#xff1a;文章有错误的地方&#xff0c;还望诸位大神不吝指教&#xff01; 文章目录 前言一、Post-Processing是什么&#xff1f;二、安装使用Post-Processing1.安装Post-Processing2.使用Post-Processing&#xff08;1&#xff09;.添加Post-process Volume&#xff08…

解析Spring Boot中的CommandLineRunner和ApplicationRunner:用法、区别和适用场景详解

在Spring Boot应用程序中&#xff0c;CommandLineRunner和ApplicationRunner是两个重要的接口&#xff0c;它们允许我们在应用程序启动后执行一些初始化任务。本文将介绍CommandLineRunner和ApplicationRunner的区别&#xff0c;并提供代码示例和使用场景&#xff0c;让我们更好…

Hive调优

1.参数配置优化 设定Hive参数有三种方式&#xff1a; &#xff08;1&#xff09;配置Hive文件 当修改配置Hive文件的设定后&#xff0c;对本机启动的所有Hive进程都有效&#xff0c;因此配置是全局性的。 一般地&#xff0c;Hive的配置文件包括两部分&#xff1a; a&#xff…

线性变换功能块S_RTI工程上的主要应用

西门子S_RTI模拟量转换功能块算法公式和代码介绍请参考下面文章链接: PLC模拟量输出 模拟量转换FC S_RTI-CSDN博客文章浏览阅读5.3k次,点赞2次,收藏11次。1、本文主要展示西门子博途模拟量输出转换的几种方法, 方法1:先展示下自编FC:计算公式如下:intput intput Real IS…

Oracle主备切换,ogg恢复方法(集成模式)

前言: 文章主要介绍Oracle数据库物理ADG主备在发生切换时(switchover,failover)&#xff0c;在主库运行的ogg进程(集成模式)如何进行恢复。 测试恢复场景&#xff0c;因为集成模式不能在备库配置&#xff0c;所以场景都是基于主库端: 1 主备发生switchover切换&#xff0c;主库…

吴恩达《机器学习》9-1-9-3:反向传播算法、反向传播算法的直观理解

一、正向传播的基础 在正向传播中&#xff0c;从神经网络的输入层开始&#xff0c;通过一层一层的计算&#xff0c;最终得到输出层的预测结果。这是一种前向的计算过程&#xff0c;即从输入到输出的传播。 二、反向传播算法概述 反向传播算法是为了计算代价函数相对于模型参数…