webservice笔记

news2024/10/6 16:30:27

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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/core
       http://cxf.apache.org/schemas/core.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
       http://cxf.apache.org/jaxrs
       http://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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/core
       http://cxf.apache.org/schemas/core.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
       http://cxf.apache.org/jaxrs
       http://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 {
	//注入对象
	@Resource
	private HelloService helloService;
	
	@Test
	public 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;
	}
	@Override
	public 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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/core
       http://cxf.apache.org/schemas/core.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
       http://cxf.apache.org/jaxrs
       http://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.coloradmin.cn/o/1227322.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

文件重命名最佳实践:如何确保文件名的准确性和一致性

在日常生活和工作中&#xff0c;经常需要处理大量的文件&#xff0c;包括电子文件和纸质文件。文件重命名是为了更好地组织和管理这些文件&#xff0c;以方便查找和使用。然而&#xff0c;重命名文件并不是一件简单的事情&#xff0c;它需要遵循一定的最佳实践以确保文件名的准…

基环树(pseudotree)入门

目录 无向基环树找环&#xff0c;[题目](https://www.luogu.com.cn/problem/P8655)拓扑排序找环并查集找环dfs找环 内向基环树[2876. 有向图访问计数](https://leetcode.cn/problems/count-visited-nodes-in-a-directed-graph/description/)[2127. 参加会议的最多员工数](https…

公司内部网络架设悟空CRM客户管理系统 cpolar无需公网IP实现内网,映射端口外网访问

1、什么是内网穿透&#xff1f; 内网穿透&#xff0c;即内网映射&#xff0c;内网IP端口映射到外网的过程。是一种主动的操作&#xff0c;需要本人一些内网的权限。比如在公司自己电脑&#xff0c;将办公OA发布到互联网&#xff0c;然后提供外网在家或出差在外连接访问。 可以…

动态改标题

<el-dialog :title"showTitle" :visible"showDialog" close"close"> </el-dialog>使用计算属性 computed: {showTitle() {//这里根据点击的是否有具体点击的那个id来判断return this.form.id ? "编辑部门" : "新增部…

Linux:安装软件的两种方式rpm和yum

一、rpm方式 1、简单介绍 RPM是RedHat Package Manager的缩写&#xff0c;它是Linux上打包和安装的工具。通过rpm打包的文件扩展名是.RPM。这个安装包就类似Windows系统中的.exe文件。rpm工具实现Linux上软件的离线安装。 2、软件相关信息的查询命令 查询Linux系统上所有已…

docker的基本使用以及使用Docker 运行D435i

1.一些基本的指令 1.1 容器 要查看正在运行的容器&#xff1a; sudo docker ps 查看所有的容器&#xff08;包括停止状态的容器&#xff09; sudo docker ps -a 重新命名容器 sudo docker rename <old_name> <new_name> <old_name> 替换为你的容器名称…

模块化Common JS 和 ES Module

目录 历程 1.几个函数&#xff1a;全局变量的污染&#xff0c;模块间没有联系 2.对象&#xff1a;暴露成员&#xff0c;外部可修改 3.立即执行函数&#xff1a;闭包实现模块私有作用域 common JS module和Module 过程 模块依赖&#xff1a;深度优先遍历、父 -> 子 -…

辅助笔记-Jupyter Notebook的安装和使用

辅助笔记-Jupyter Notebook的安装和使用 文章目录 辅助笔记-Jupyter Notebook的安装和使用1. 安装Anaconda2. conda更换清华源3. Jupter Notebooks 使用技巧 笔记主要参考B站视频“最易上手的Python环境配置——Jupyter Notebook使用精讲”。 Jupyter Notebook (此前被称为IPyt…

C++初阶 日期类的实现(上)

目录 一、前置准备 1.1获得每月的天数 1.2获得每年的天数 1.3构造函数&#xff0c;析构函数和拷贝构造函数 二、日期与天数的,-,,-实现 2.1运算符重载 2.2运算符的实现 2.3-运算符的实现 2.4-运算符的实现 三、&#xff0c;--的实现 3.1前置&#xff0c;后置的实现 …

《洛谷深入浅出基础篇》P5266 学籍管理——map的应用

上链接&#xff1a;P5266 【深基17.例6】学籍管理 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P5266#submit 题干&#xff1a; 题目描述 您要设计一个学籍管理系统&#xff0c;最开始学籍数据是空的&#xff0c;然后该系统能够支持下面的…

Vue3--Vue Router详解--学习笔记

1. 认识vue-router Angular的ngRouter React的ReactRouter Vue的vue-router Vue Router 是Vue.js的官方路由&#xff1a; 它与Vue.js核心深度集成&#xff0c;让Vue.js构建单页应用&#xff08;SPA&#xff09;变得非常容易&#xff1b;目前Vue路由最新的版本是4.x版本。 v…

德迅云安全告诉您 网站被攻击怎么办-SCDN来帮您

随着互联网的发展给我们带来极大的便利&#xff0c;但是同时也带来一定的安全威胁&#xff0c;网络恶意攻击逐渐增多&#xff0c;很多网站饱受困扰&#xff0c;而其中最为常见的恶意攻击就是cc以及ddos攻击。针对网站攻击&#xff0c;今天为您介绍其中一种防护方式-SCDN&#x…

计算机毕业设计选题推荐-点餐微信小程序/安卓APP-项目实战

✨作者主页&#xff1a;IT毕设梦工厂✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Py…

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

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

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

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

argocd

部署argocd https://github.com/argoproj/argo-cd/releases kubectl create namespace argocd kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/v2.9.1/manifests/install.yaml官网 https://argo-cd.readthedocs.io/en/stable/ kubectl crea…

基于灰色神经网络的预测算法——订单需求预测

大家好&#xff0c;我是带我去滑雪&#xff01; 灰色系统理论的不确定性处理与神经网络的非线性建模相结合&#xff0c;有望更好地处理实际问题中的不确定性和复杂性。本期使用灰色神经网络实现预测冰箱订单需求。 一、问题背景与模型建立 &#xff08;1&#xff09;灰色理论…

物联网AI MicroPython学习之语法 TIMER硬件定时器

学物联网&#xff0c;来万物简单IoT物联网&#xff01;&#xff01; TIMER 介绍 模块功能: 硬件定时器模块 接口说明 Timer - 构建Timer对象 函数原型&#xff1a;Timer(id)参数说明&#xff1a; 参数类型必选参数&#xff1f;说明idintY硬件定时器外设模块id&#xff1a…

计算机毕业设计项目选题推荐(免费领源码)java+Springboot+mysql高校自习室座位预约管理系统67512

摘 要 信息化社会内需要与之针对性的信息获取途径&#xff0c;但是途径的扩展基本上为人们所努力的方向&#xff0c;由于站在的角度存在偏差&#xff0c;人们经常能够获得不同类型信息&#xff0c;这也是技术最为难以攻克的课题。针对高校自习室座位预约管理系统等问题&#xf…