【Web Service】:IDEA+Spring Boot,Web Service服务端和客户端开发_idea webservice springboot-程序员宅基地

技术标签: spring boot  java  intellij-idea  Web Service  

一、服务端开发

1、pom文件依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.6</version>
        </dependency>

2、接口

package com.example.demo.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(name = "TestService", // 暴露服务名称
        targetNamespace = "http://ws.demo.example.com"// 命名空间,一般是接口的包名倒序
)
public interface TestService {
    @WebMethod
    public String sayHello(@WebParam(name = "name") String name);

}

注意:接口的targetNamespace 要和接口实现类的targetNamespace 一致!!!

3、接口实现类

package com.example.demo.ws;

import org.springframework.stereotype.Component;

import javax.jws.WebService;

@WebService(serviceName = "TestService", // 与接口中指定的name一致
        targetNamespace = "http://ws.demo.example.com", // 与接口中的命名空间一致
        endpointInterface = "com.example.demo.ws.TestService"// 接口地址
)
@Component
public class TestServiceImpl implements TestService {
    @Override
    public String sayHello(String name) {
        System.out.println("服务端被调用!");
        return name + ",你好!";
    }
}

4、Web Service配置类

package com.example.demo.ws;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {
    @Bean
    public ServletRegistrationBean disServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webService/*");
        return servletRegistrationBean;
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), new TestServiceImpl());
        endpoint.publish("/TestService");
        return endpoint;
    }

}

5、启动项目查看所写接口:http://localhost:8080/webService

接口的WSDL文件:http://localhost:8080/webService/TestService?wsdl

WSDL 是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言

至此,服务端开发完成。

二、客户端开发

如何在其它项目调用前面所写的Web Service接口???

需要将WSDL转Java代码。

 ​​​​http://localhost:8080/webService/TestService?wsdl  这是接口的WSDL文件地址。

也可以把内容复制出来,放在文本文档,再保存为wsdl后缀的文件。

1、新建另一个项目:

因为这里用到是axis,不加以下依赖会出现异常:

java.lang.ClassNotFoundException: org.apache.axis.wsdl.WSDL2Java

        <dependency>
            <groupId>org.lucee</groupId>
            <artifactId>axis</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.lucee</groupId>
            <artifactId>jaxrpc</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.5</version>
        </dependency>

2、添加Web Service 客户端支持

项目名处右键-Add Framework Support... 。添加框架支持

 添加Web Service Clinet支持,按照图中选项,ok即可。

3、将wsdl转java代码

Tools->WebServices->Generate Java Code From Wsdl...

Then:

于是生成一堆文件:

4、接口调用

HelloWorldClient.java

package example;

import ws.TestService_PortType;
import ws.TestService_ServiceLocator;

public class HelloWorldClient {
    public static void main(String[] argv) {
        try {
            TestService_ServiceLocator factory = new TestService_ServiceLocator();
            TestService_PortType bizorgServiceImplPort = factory.getTestServiceImplPort();
            String result = bizorgServiceImplPort.sayHello("王富贵");
            System.out.println(result);
        } catch (javax.xml.rpc.ServiceException ex) {
            ex.printStackTrace();
        } catch (java.rmi.RemoteException ex) {
            ex.printStackTrace();
        }
    }
}

 接口返回:

 

服务端:

over。

---------------------------------------------------------------------------------------------------------------------------------

服务端的开发有参考SpringBoot——实现WebService接口服务端以及客户端开发_Archie_java的博客-程序员宅基地_springboot webservice接口开发客户端的开发完全自己试错过程。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43512797/article/details/124932250

智能推荐

[蓝桥杯2019初赛]A组_给定一个长度为 n 的数组,从小到大输出任何至少出现三次的值,如果没有这样的值,则-程序员宅基地

题目描述给定一个长度为N 的数组A = [A1, A2,…,AN],数组中有可能有重复出现的整数。现在小明要按以下方法将其修改为没有重复整数的数组。小明会依次修改A2,A3,…, AN。当修改Ai 时,小明会检查Ai 是否在A1~ Ai-1 中出现过。如果出现过,则小明会给Ai 加上1 ;如果新的Ai 仍在之前出现过,小明会持续给Ai加1 ,直到Ai没有在A1~ Ai-1中出现过。当..._给定一个长度为 n 的数组,从小到大输出任何至少出现三次的值,如果没有这样的值,则

解决springmvc不会在Artifacts里面导包的错误_springmvcconfig导不了包-程序员宅基地

这个原因很迷,因为springmvc不会在Artifacts导入我们所需要的包,所以导致我们老是报404错误,解决方法在Artifacts里新建一个lib然后导入我们的library_springmvcconfig导不了包

4种常见的缓存模式,你都知道吗?-程序员宅基地

概述 在系统架构中,缓存可谓提供系统性能的最简单方法之一,稍微有点开发经验的同学必然会与缓存打过交道,最起码也实践过。如果使用得当,缓存可以减少响应时间、减少数据库负载以及节省成本。但如果缓存使用不当,则可能出现一些莫名其妙的问题。在不同的场景下,所使用的缓存策略也是有变化的。如果在你的印象和经验中,缓存还只是简单的查询、更新操作,那么这篇文章真的值得你学习一下。在这里,..._缓存模式

Hadoop分布式集群的搭建_分布式服务器集群搭建_Vic·Tory的博客-程序员宅基地

大数据相关概念,Hadoop3搭建分布式集群。_分布式服务器集群搭建

nginx中的超时设置,请求超时、响应等待超时等-程序员宅基地

nginx比较强大,可以针对单个域名请求做出单个连接超时的配置.比如些动态解释和静态解释可以根据业务的需求配置proxy_connect_timeout:后端服务器连接的超时时间_发起握手等候响应超时时间proxy_read_timeout:连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)proxy_send_timeout:后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据nginx使用proxy模块...

Python 的描述符 descriptor详解_python descriptor-程序员宅基地

Python 在 2.2 版本中引入了descriptor(描述符)功能,也正是基于这个功能实现了新式类(new-styel class)的对象模型,同时解决了之前版本中经典类 (classic class) 系统中出现的多重继承中的 MRO(Method Resolution Order) 问题,另外还引入了一些新的概念,比如 classmethod, staticmethod, super, Property 等。因此理解 descriptor 有助于更好地了解 Python 的运行机制。那么什么是_python descriptor

随便推点

多线程编程基础(线程创建)-程序员宅基地

转载自:http://blog.csdn.net/bertzhang/article/details/72190601、多线程的创建线程的创建比较简单,先举一个例子热热身:[cpp] view plaincopy#include #include void* Handler(void* param) {

微信小程序之工具下载安装以及介绍_微信小程序模拟器下载_忘川茶社的博客-程序员宅基地

小程序开发与实战学习视频:https://www.bilibili.com/video/BV1Gv411g7j6?p=1项目介绍十方智育,商业级项目功能实现:微信小程序端的页面实现 + json-server(数据模拟)项目资源资源下载地址:ftp://192.168.92.173账号:c64密码:123456素材微信小程序官方网站:https://mp.weixin.qq.com/开发工具:官方提供的微信小程序开发工具安装注意事项:尽量避免中文目录、空格、特殊符号(当然,下划线还_微信小程序模拟器下载

IT互联网行业各岗位介绍及知识普及_爱学习Java的靓女的博客-程序员宅基地

一、首先,我们先看一看整个IT行业岗位的一个大致的分类:  产品策划类:互联网产品经理、产品运营  页面设计类:UI交互设计、移动UI设计  前端与移动:Web前端开发、HTML5、安卓、iOS  开发与测试:PHP、Java、python、软件测试  营销推广类:SEO、SEM、新媒体、电商运营  数据运营类:数据分析、数据挖掘、大数据  运营维护类:运维管理、linux  游戏相关类:VR/AR、Unity游戏开发  二、那对于小白来说,可以对于这个科目都不太了解,那我简单介绍几个吧

python 遍历文件夹及文件-程序员宅基地

# -*- coding: cp936 -*-import osimport os.pathimport ConfigParser rootdir = r"D:\Project" # 指明被遍历的文件夹PathList=list()PathList_update=list()PathDict=dict()PathLi

Java :JDK环境变量配置_jdk的减号和数字之间有空格吗-程序员宅基地

JDK 安装和配置完成后,可以测试其是否能够正常运行。选择“开始”|“运行”命令,在打开的“运行”对话框中输入 cmd 命令,按 Enter 键进入到 DOS 环境下。在命令提示符后输入并执行 java -version 命令,系统如果输出类似图 1 所示的 JDK 版本信息,说明 JDK 已经配置成功。图1 查看JDK版本提示:在命令提示符后输入测试命令时,需要注意 java 和..._jdk的减号和数字之间有空格吗

解决C盘根目录不能创建文件,只能创建文件夹问题_mkditr为什么不能再根目录创建-程序员宅基地

转载:https://blog.csdn.net/xinke453/article/details/7496545解决方法用管理员运行cmd 输入icacls c:\ /setintegritylevel M运行完没有什么提示,关闭窗口即可。再创建文件发现就可以了。..._mkditr为什么不能再根目录创建