Spring Boot集成webservice快速入门demo

1.什么是webservice?

WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。 WebService实现不同语言间的调用,是依托于一个标准,webservice是需要遵守WSDL(web服务定义语言)/SOAP(简单请求协议)规范的。 WebService=WSDL+SOAP+UDDI(webservice的注册)Soap是由Soap的part和0个或多个附件组成,一般只有part,在part中有Envelope和Body。 Web Service是通过提供标准的协议和接口,可以让不同的程序集成的一种SOA架构

Web Service的优点

  • 可以让异构的程序相互访问(跨平台)
  • 松耦合
  • 基于标准协议(通用语言,允许其他程序访问)

Web Service的基本原理

  • Service Provider采用WSDL描述服务
  • Service Provider 采用UDDI将服务的描述文件发布到UDDI服务器(Register server)
  • Service Requestor在UDDI服务器上查询并 获取WSDL文件
  • Service requestor将请求绑定到SOAP,并访问相应的服务。

什么是SOAP?

SOAP请求(Simple Object Access Protocol,简单对象访问协议)是HTTP POST的一个专用版本,遵循一种特殊的XML消息格式,Content-type设置为:text/xml ,任何数据都可以XML化。 SOAP:简单对象访问协议。SOAP是一种轻量的,简单的,基于XML的协议,它被设计成在web上交换结构化的和固化的信息。SOAP可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。

2.代码工程

实验目标:

实现webservice服务,并通过client调用服务端

服务端

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>webservice</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>webservice-server</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 引入Spring Boot Web Services Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <!-- 引入Apache CXF Spring Boot Starter for JAX-WS -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.4</version>
        </dependency>


    </dependencies>
</project>
config
package com.et.webservice.server.config;

import com.et.webservice.server.service.MyWebService;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * CXF配置类,负责初始化CXF相关组件、发布Webservice服务以及配置CXF Servlet。
 */
@Configuration
public class CxfConfig {
   

    /**
     * 自动注入Spring Bus实例,它是CXF的核心组件之一,用于管理和配置CXF运行时环境。
     */
    @Autowired
    private SpringBus bus;

    /**
     * 自动注入实现了MyWebService接口的服务实现类实例,该实例将被发布为Webservice供外部调用。
     */
    @Autowired
    private MyWebService myWebServiceImpl;

    /**
     * 创建并返回Webservice端点(Endpoint)实例,用于发布MyWebService服务。
     * 将服务实现类与Spring Bus关联,并指定发布地址为"/1"。
     *
     * @return Webservice端点实例
     */
    @Bean
    public EndpointImpl endpoint() {
   

        EndpointImpl endpoint = new EndpointImpl(bus, myWebServiceImpl);
        endpoint.publish("/1"); // 发布地址
        return endpoint;
    }

    /**
     * 创建并返回CXF Servlet的ServletRegistrationBean实例,用于注册CXF Servlet到Spring Boot的Servlet容器中。
     * 设置CXF Servlet的映射路径为"/services/*",表示所有以"/services/"开头的HTTP请求都将由CXF Servlet处理。
     *
     * @return CXF Servlet的ServletRegistrationBean实例
     */
    @Bean
    public ServletRegistrationBean wsServlet() {
   
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }
}
service
package com.et.webservice.server.service;


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

@WebService(
        name = "MyWebService",
        targetNamespace = "http://liuhaihua.cn/mywebservice"
)
public interface MyWebService {
   
    @WebMethod
    String sayHello(String name);
}
package com.et.webservice.server.service;


import org.springframework.stereotype.Service;

import javax.jws.WebService;


@Service
@WebService
public class MyWebServiceImpl implements MyWebService {
   

    @Override
    public String sayHello(String name) {
   
        System.err.println("sayHello is called..."); // 只是为了更明显的输出,采用err

        return "Hello, " + name + "!";
    }
}

DemoApplication.java

package com.et.webservice.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

客户端

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>webservice</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>webservice-client</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 测试依赖配置 -->
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.9.1</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>

        <!-- 主要依赖配置 -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>4.0.5</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>jakarta.activation</groupId>
            <artifactId>jakarta.activation-api</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>jakarta.jws</groupId>
            <artifactId>jakarta.jws-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.ws</groupId>
            <artifactId>jakarta.xml.ws-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <!-- Apache CXF相关依赖 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-reload4j</artifactId>
            <version>2.1.0-alpha1</version>
        </dependency>
    </dependencies>
</project>
service
package com.et.webservice.client;

import javax.jws.WebService;

@WebService(
      name = "MyWebService",
      targetNamespace = "http://liuhaihua.cn/mywebservice"
)
public interface HelloService {
    // 接口名一样

    String sayHello(String name); // 方法定义名一样
}
cilent
package com.et.webservice.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

/**
 * 客户端调用类,用于通过JAX-WS代理方式访问HelloService Web服务。
 */
public class Client {
   

    /**
     * 程序主入口方法。
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
   
        // 创建JAX-WS代理工厂对象
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();

        // 设置要访问的服务地址
        jaxWsProxyFactoryBean.setAddress("http://localhost:8088/services/1?wsdl");

        // 设置服务接口类,即HelloService
        jaxWsProxyFactoryBean.setServiceClass(HelloService.class);

        // 使用工厂对象创建HelloService接口的代理实例
        HelloService helloService = jaxWsProxyFactoryBean.create(HelloService.class);

        System.out.println(helloService.getClass());

        // 调用代理实例的方法,向服务端发送请求,并打印返回结果
        System.out.println(helloService.sayHello("hello world"));
    }
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

3.测试

启动服务端,访问http://localhost:8088/services/1?wsdl

111

调用客户端,返回结果

11:20:20.148 [main] DEBUG org.apache.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on interceptor org.apache.cxf.ws.policy.PolicyVerificationInInterceptor@1e8823d2
11:20:20.148 [main] DEBUG org.apache.cxf.ws.policy.PolicyVerificationInInterceptor - Verified policies for inbound message.
Hello, hello world!

4.引用

 

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以使用JAX-WS或者Spring Web Services(Spring-WS)来调用SOAP Web Service接口,也可以使用RestTemplate来调用RESTful Web Service接口。 以下是使用Spring-WS调用SOAP Web Service接口的步骤: 1. 引入Spring-WS和JAXB相关依赖 ```xml <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置WebServiceTemplate 在配置类中添加WebServiceTemplate的Bean,并设置WebServiceTemplate的Marshaller和Unmarshaller,这里使用Jaxb2Marshaller ```java @Configuration public class WebServiceConfig { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("com.example.webservice.demo.wsdl"); return marshaller; } @Bean public WebServiceTemplate webServiceTemplate() { WebServiceTemplate template = new WebServiceTemplate(); template.setMarshaller(marshaller()); template.setUnmarshaller(marshaller()); template.setDefaultUri("http://localhost:8080/ws"); return template; } } ``` 3. 调用WebService 使用WebServiceTemplate的marshalSendAndReceive方法来发送SOAP请求并接收响应,示例代码如下: ```java @Autowired private WebServiceTemplate webServiceTemplate; public void callWebService() { GetCountryRequest request = new GetCountryRequest(); request.setName("Spain"); GetCountryResponse response = (GetCountryResponse) webServiceTemplate.marshalSendAndReceive(request); System.out.println(response.getCountry().getCapital()); } ``` 以上就是使用Spring-WS调用SOAP Web Service接口的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值