springboot开发webservice服务端和客户端代码

直接上工程代码

首先创建springboot项目,这个就不多说了。整体项目结构如下图:

在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rh</groupId>
    <artifactId>ws_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ws_test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- 核心启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- web启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- webService-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.1</version>
        </dependency>
        <!-- CXF webservice -->
		
		<!-- 一下是客户端开发所需jar包 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.18.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.xml/jaxrpc -->
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxrpc</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.0.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

服务端代码:

ServerServiceDemo接口代码:

package com.rh.ws_test;

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

@WebService(name = "ServerServiceDemo", targetNamespace = "http://server.webservice.example.com")
public interface ServerServiceDemo {
    @WebMethod
    String emrService(@WebParam String data);

}

ServerServiceDemoImpl接口实现类代码:

package com.rh.ws_test;

import org.springframework.stereotype.Component;

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

/**
 * WebService涉及到的有这些 "四解三类 ", 即四个注解,三个类
 * @WebMethod
 * @WebService
 * @WebResult
 * @WebParam
 * SpringBus
 * Endpoint
 * EndpointImpl
 *
 * 一般我们都会写一个接口,然后再写一个实现接口的实现类,但是这不是强制性的
 * @WebService 注解表明是一个webservice服务。
 *      name:对外发布的服务名, 对应于<wsdl:portType name="ServerServiceDemo"></wsdl:portType>
 *      targetNamespace:命名空间,一般是接口的包名倒序, 实现类与接口类的这个配置一定要一致这种错误
 *              Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name xxxx
 *              对应于targetNamespace="http://server.webservice.example.com"
 *      endpointInterface:服务接口全路径(如果是没有接口,直接写实现类的,该属性不用配置), 指定做SEI(Service EndPoint Interface)服务端点接口
 *      serviceName:对应于<wsdl:service name="ServerServiceDemoImplService"></wsdl:service>
 *      portName:对应于<wsdl:port binding="tns:ServerServiceDemoImplServiceSoapBinding" name="ServerServiceDemoPort"></wsdl:port>
 *
 * @WebMethod 表示暴露的服务方法, 这里有接口ServerServiceDemo存在,在接口方法已加上@WebMethod, 所以在实现类中不用再加上,否则就要加上
 *      operationName: 接口的方法名
 *      action: 没发现又什么用处
 *      exclude: 默认是false, 用于阻止将某一继承方法公开为web服务
 *
 * @WebResult 表示方法的返回值
 *      name:返回值的名称
 *      partName:
 *      targetNamespace:
 *      header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中
 *
 * @WebParam
 *       name:接口的参数
 *       partName:
 *       targetNamespace:
 *       header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中
 *       model:WebParam.Mode.IN/OUT/INOUT
 */
@Component
@WebService(name = "ServerServiceDemo", targetNamespace = "http://server.webservice.example.com",
        endpointInterface = "com.rh.ws_test.ServerServiceDemo")
public class ServerServiceDemoImpl implements ServerServiceDemo{

    @Override
    public String emrService(@WebParam String data) {
        if(null == data || "".equals(data.trim())){
            return "传入的参数为空";
        }
        return "调用成功";
    }
}

WebServiceConfig配置代码:

package com.rh.ws_test;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * 注意:
 * org.apache.cxf.Bus
 * org.apache.cxf.bus.spring.SpringBus
 * org.apache.cxf.jaxws.EndpointImpl
 * javax.xml.ws.Endpoint
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private ServerServiceDemo serverServiceDemo;

    /**
     * Apache CXF 核心架构是以BUS为核心,整合其他组件。
     * Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的ApplicationContext,这些共享资源包括
     * WSDl管理器、绑定工厂等。通过对BUS进行扩展,可以方便地容纳自己的资源,或者替换现有的资源。默认Bus实现基于Spring架构,
     * 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认
     * 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。
     * 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
     * 此方法被注释后, 即不改变前缀名(默认是services), wsdl访问地址为 http://127.0.0.1:8080/services/ws/api?wsdl
     * 去掉注释后wsdl访问地址为:http://127.0.0.1:8080/soap/ws/api?wsdl
     * http://127.0.0.1:8080/soap/列出服务列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看实际的服务
     * 新建Servlet记得需要在启动类添加注解:@ServletComponentScan
     *
     * 如果启动时出现错误:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
     * 可能是springboot与cfx版本不兼容。
     * 同时在spring boot2.0.6之后的版本与xcf集成,不需要在定义以下方法,直接在application.properties配置文件中添加:
     * cxf.path=/service(默认是services)
     */
    //@Bean
    //public ServletRegistrationBean dispatcherServlet() {
    //    return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    //}

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

启动项目后访问:
http://localhost:8083/services/

在这里插入图片描述

客户端代码:

TestSoap代码:

package com.rh.ws_test.client;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;

public class TestSoap {
    public static void main(String[] args) throws Exception {
        //一般如果没有注解targetNamespace的话,默认生成的就是接口这个文件的路径名
//        String nameSpaceURI = "http://service.cm.com";
        String nameSpaceURI = "http://server.webservice.example.com";
//        String publishUrl = "http://localhost:8087/services/ServiceMonitor?wsdl";
        String publishUrl = "http://localhost:8083/services/ws/api?wsdl";
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(new URL(publishUrl));
        //指定接口路径,要调用的方法名
//        call.setOperationName(new QName(nameSpaceURI, "monitor"));
        call.setOperationName(new QName(nameSpaceURI, "emrService"));
        //如果没用@WebParam(name="name")来表明参数名,则方法的入参是啥,这边就必须传一样的参数名才行。不然报错。
        call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
//        call.addParameter("age", XMLType.XSD_INT, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        String name = "zhanglifeng";
        int age = 18;
        Object[] obj = new Object[] { name};
        String result = (String) call.invoke(obj);

        System.out.println(result);
    }
}

参考:
包含服务端和客户端的开发(我开发的客户端不是这里的客户端)
https://www.cnblogs.com/myitnews/p/12370308.html

启动springboot项目时可能报错的解决办法:
https://blog.csdn.net/llllllkkkkkooooo/article/details/107760593

客户端调用的时候,可能报的错误:
https://blog.csdn.net/hymas/article/details/78086230?utm_source=blogxgwz8

注意:springboot开发webservice和axis组件开发webservice用法上有点区别

原文地址:https://blog.csdn.net/Brave_heart4pzj/article/details/110916127

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXObject){xhr=newActiveXObject("Microsoft.XMLHTTP");}else{xhr=newXMLHttpRequest();}//指定请求地址varurl="http://127.0.0.1:7777/hello?wsdl";//
               好不容易把WebService服务器端搭建起来,我们还需要客户端程序也同样跑起来才能够进行和服务器端程序的通信: 在这篇文章里面,我会先自己写代码去实现调用WebService服务器端程序,接下来,通过IDEA配置的方式来调用WebService服务端: 首先,我写了一个W
1新建一个工程项目用来做服务端增加一个MyService1类文件packagecom.zns.ws;importjavax.jws.WebMethod;importjavax.jws.WebService;importjavax.xml.ws.Endpoint;@WebServicepublicclassMyService1{publicstaticvoidmain(String[]args){
packagecom.transsion.util;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.URL;importjava.net.URLConnection;importcom.alibaba.druid.util.Base64;importcom.tra
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Web;5usingSystem.Web.Services.Description;67namespaceStrongSoftManage.Web.App8{9publicclassSoapExtens:SoapExtensi
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生成2个类:ContractConnector.java packagecom.wonders.webservice.contract;importjava.text.DecimalFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;i
一、WebService概述1.1什么是WebService 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 一个跨语言、跨平台的规范(抽象)所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务
一、什么是CXF?    ApacheCXF=Celtix+Xfire,开始叫ApacheCeltiXfire,后来更名为ApacheCXF了,以下简称为CXF。ApacheCXF是一个开源的webServices框架,CXF帮助您构建和开发webServices,它支持多种协议,比如:SOAP1.1,1,2 XML/HTTP、RESTful或者CORBA。  
工具IDEA一、构建项目1、选择SpringInitializr2、填写项目信息3、勾选webService4、勾选Thymeleaf5、项目建立完成,启动类自动生成二、写个Controller启动服务。浏览器访问/hello接口。 
1.环境win764位,vs20102.新建一个asp.netweb应用程序(同上一篇)3.添加一个web引用,引用上一篇创建的服务。注意不是服务引用。如下图 
WebService之WSDL文件讲解   恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时候,走的是SOAP协议,然后用户给你一个WSDL文件,说按照上面的进行适配,嘿嘿,这个时候,要是你以前没有开发过,肯定会傻眼,那如果你想学习的话,就认真的看下面的讲解咯:一、WSDL概述  
在websrvice发布文件的webconfig中加入<httpRuntimemaxRequestLength="102400"/> <webServices>     <protocols>       <addname="HttpPost"/>       <addname="HttpGet"/>     </protocols>   
 代码比较简单,按照如下来操作即可,只是jar包有很多问题,比如找不到classnotFondspring、以及找不到xfile.xml、以及xfile.xml中的一个参数问题,以及2.0 spring。jar和spring1.6.2冲突问题,总之这个小demo报了一堆错误,其实都是jar的问题,为了让大家减少这方面的错误,所以我提供
 一、soapUI简介SOAP:   WebService通过Http协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息头的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用WebService。 
参考,感谢https://blog.csdn.net/hj7jay/article/details/727224381.环境:win764位,jdk1.8.0_201 EclipseJavaEEIDEforWebDevelopers.Version:Mars.1Release(4.5.1)2.创建一个普通的java项目,名字是TheService在src目录下创建一个com.hyan.service包,在此包下创建
CXF实现RestfulWebService基础示例一、写在前面IDE:IDEA14JDK:1.7CXF:2.6.2示例来源:%CXF_HOME%\samples\jax_rs\basic发布方式:JAXRSServerFactoryBean的create()方法调用方式:URL的openStream()方法、HttpClient的executeMethod()方法二、服务端(Java项目)1.准备Jar包
封装helper类:classWebServiceHelper{///<summary>///1.get请求http方法///</summary>///<paramname="url">基础url</param>///<paramname="method">请求方法</param>///<paramnam
.net客户端调用java或.netwebservice进行soapheader验证最近项目中有业务需要跨平台调用web服务,客户端和服务器之间采用非对称加密来保证数据的安全性,webservice的安全验证基于soapheader。借此机会,顺便整理一下调用.netwebservice和javawebservice的验证方式,记录下来。
Node.jshttps://www.cnblogs.com/goldlong/p/8027997.htmlQQ音乐apihttps://juejin.im/post/5a35228e51882506a463b172#heading-11?tdsourcetag=s_pcqq_aiomsgGit把本地仓库上传到GitHbubhttps://blog.csdn.net/zamamiro/article/details/70172900git删除本地仓库https://blog.cs
转载自:孤傲苍狼 WebService学习总结(三)——使用JDK开发WebService一、WebService的开发手段使用Java开发WebService时可以使用以下两种开发手段1、 使用JDK开发(1.6及以上版本)-->详见:本文2、使用CXF框架开发-->详见:其他文章二、使用JDK开发WebServi