百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

WebService入门

yuyutoo 2024-12-06 20:39 1 浏览 0 评论

一、什么是WebService

WebService是部署在Web上的,可访问的应用程序。主要是对外提供业务接口(业务功能)。

日常生活中的WebService:比如:天气预告、查询手机归属地。

二、WebService的组成


说明:

  • 注册方:表示服务都要在注册服务器中进行注册,比如:服务的地址、接口、接口的方法等;
  • 服务提供者:服务的实现方;
  • 服务消费者:服务的调用方(客户端),广义来讲:Web应用,APP、组件等等。

三、WebService技术标准

  • xml技术,web服务具体平台无关性、语言无关系;
  • soap协议, Simple Object Access简单对象访问协议;
  • wsdl web服务的描述语言(xml文档),用于描述web服务

Dubbo

四、Restful服务(重点)

  • json数据格式的交互
  • http协议
  • 无需wsdl

Springcloud+Springboot

五、开发传统WebService

Apache CXF=Celtix+Xfire,开始叫Apache CeltiXfire,后来更名为Apache CXF了,以下简称为CXF。Apache CXF。Apache CXF是一个开源的Web Services框架,CXF帮助您构建和开发 web Service,它支持多种协议,比如:SOAP1.1,1.2XML/HTTP、RESTful或者CORBA。

? CXF是基于SOA总线结构,依靠Spring完成模块的集成,实现SOA方式。

六、开发WebService的步骤:

服务端


①创建服务端Web项目,导入CXF的类库。




配置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">

    <!-- CXF 中央控制器 -->
    <servlet>
        <servlet-name>cxfServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </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:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

</web-app>

创建实体类

package com.gec.ws.pojo;

import java.io.Serializable;

public class Product implements Serializable {
    private Integer id;
    private String name;
    private Double price;
    private String address;


    public Product() {
    }

    public Product(Integer id, String name, Double price, String address) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

创建ProductService接口

package com.gec.ws.service;

import com.gec.ws.pojo.Product;

import javax.jws.WebService;
import java.util.List;

@WebService //当前是一个可发布的web接口
public interface ProductService {
    public List<Product> findAllProducts();
}

创建ProductServiceImpl实现类

package com.gec.ws.service;

import com.gec.ws.pojo.Product;
import com.gec.ws.service.ProductService;

import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    @Override
    public List<Product> findAllProducts() {
        List<Product> list=new ArrayList<>();
        for (int i=1;i<11;i++){
            list.add(new Product(i,"红米手机"+i,7000D,"广州"));
        }
        return list;
    }
}

创建applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
    xmlns="http://www.springframework.org/schema/beans">

	<!-- 发布应用程序接口 -->
	<jaxws:endpoint id="productServiceImpl" 
	                implementor="com.gec.ws.service.ProductServiceImpl"
	                address="/productService"></jaxws:endpoint>
	
</beans>

配置tomcat



⑧运行



点击{http://service.ws.gec.com/}ProductServiceImplService后跳转的页面


客户端


创建客户端web项目,导入CXF的类库。

把服务端的实体类和接口与包一起拷贝过来

Product类

package com.gec.ws.pojo;

import java.io.Serializable;

public class Product implements Serializable {
    private Integer id;
    private String name;
    private Double price;
    private String address;


    public Product() {
    }

    public Product(Integer id, String name, Double price, String address) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

ProductService接口

package com.gec.ws.service;

import com.gec.ws.pojo.Product;

import javax.jws.WebService;
import java.util.List;

@WebService //当前是一个可发布的web接口
public interface ProductService {
    public List<Product> findAllProducts();
}

创建com.gec.ws.main包

以Spring的方式创建远程方法调用的客户端(代理)

package com.gec.ws.main;

import com.gec.ws.pojo.Product;
import com.gec.ws.service.ProductService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

//怎样区分远程方法调用(边界),以JVM为标准
public class SpringClientTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

        //远程方法调用的代理接口实例
        ProductService productService= (ProductService) context.getBean("productService");
        List<Product> list=productService.findAllProducts();
        for (Product product : list) {
            System.out.println(product.getId()+", "+product.getName()+", "+product.getPrice()+", "+product.getAddress());
        }
    }
}

以代理的方式创建远程方法

package com.gec.ws.main;

import com.gec.ws.pojo.Product;
import com.gec.ws.service.ProductService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import java.util.List;

public class ProxyClientTest {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();
        factory.setServiceClass(ProductService.class);
        factory.setAddress("http://localhost:8088/services/productService");
        
        ProductService productService= (ProductService) factory.create();
        List<Product> list=productService.findAllProducts();
        for (Product product : list) {
            System.out.println(product.getId()+", "+product.getName()+", "+product.getPrice()+", "+product.getAddress());
        }
    }

}

运行以Spring方式创建的远程方法



运行以代理方式创建的远程方法



没用到WebService之前调用远程方法的写法

package com.gec.ws.controller;

import com.gec.ws.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;  //远程方法调用webservice
}

相关推荐

如何在HTML中使用JavaScript:从基础到高级的全面指南!

“这里是云端源想IT,帮你...

推荐9个Github上热门的CSS开源框架

大家好,我是Echa。...

前端基础知识之“CSS是什么?”_前端css js

...

硬核!知网首篇被引过万的论文讲了啥?作者什么来头?

整理|袁小华近日,知网首篇被引量破万的中文论文及其作者备受关注。知网中心网站数据显示,截至2021年7月23日,由华南师范大学教授温忠麟等人发表在《心理学报》2004年05期上的学术论文“中介效应检验...

为什么我推荐使用JSX开发Vue3_为什么用vue不用jquery

在很长的一段时间中,Vue官方都以简单上手作为其推广的重点。这确实给Vue带来了非常大的用户量,尤其是最追求需求开发效率,往往不那么在意工程代码质量的国内中小企业中,Vue占据的份额极速增长...

【干货】一文详解html和css,前端开发需要哪些技术?
【干货】一文详解html和css,前端开发需要哪些技术?

网站开发简介...

2025-02-20 18:34 yuyutoo

分享几个css实用技巧_cssli

本篇将介绍几个css小技巧,目录如下:自定义引用标签的符号重置所有标签样式...

如何在浏览器中运行 .NET_怎么用浏览器运行代码

概述:...

前端-干货分享:更牛逼的CSS管理方法-层(CSS Layers)

使用CSS最困难的部分之一是处理CSS的权重值,它可以决定到底哪条规则会最终被应用,尤其是如果你想在Bootstrap这样的框架中覆盖其已有样式,更加显得麻烦。不过随着CSS层的引入,这一...

HTML 基础标签库_html标签基本结构
HTML 基础标签库_html标签基本结构

HTML标题HTML标题(Heading)是通过-...

2025-02-20 18:34 yuyutoo

前端css面试20道常见考题_高级前端css面试题

1.请解释一下CSS3的flexbox(弹性盒布局模型),以及适用场景?display:flex;在父元素设置,子元素受弹性盒影响,默认排成一行,如果超出一行,按比例压缩flex:1;子元素设置...

vue引入外部js文件并使用_vue3 引入外部js

要在Vue中引入外部的JavaScript文件,可以使用以下几种方法:1.使用``标签引入外部的JavaScript文件。在Vue的HTML模板中,可以直接使用``标签来引入外部的JavaScrip...

网页设计得懂css的规范_html+css网页设计

在初级的前端工作人员,刚入职的时候,可能在学习前端技术,写代码不是否那么的规范,而在工作中,命名的规范的尤为重要,它直接与你的代码质量挂钩。网上也受很多,但比较杂乱,在加上每年的命名都会发生一变化。...

Google在Chrome中引入HTML 5.1标记

虽然负责制定Web标准的WorldWideWebConsortium(W3C)尚未宣布HTML5正式推荐规格,而Google已经迁移到了HTML5.1。即将发布的Chrome38将引入H...

HTML DOM 引用( ) 对象_html中如何引用js

引用对象引用对象定义了一个同内联元素的HTML引用。标签定义短的引用。元素经常在引用的内容周围添加引号。HTML文档中的每一个标签,都会创建一个引用对象。...

取消回复欢迎 发表评论: