飞道的博客

springboot 整合 webservice服务

454人阅读  评论(0)


在前一段时间用到了webservice服务,今天整理了一下,记录下来。

webservice百科

Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的交互操作的应用程序。 [1]
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。
简单来说就是可以让各个系统相互调用,获取信息。以前用的很多,现在不是很常见,一般都是与一些老系统交互才会用到。

创建一个springboot项目并导入相关依赖

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

编写webservice接口

package com.jpl.service;

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

/**
* @author xla
* @date 2023-01-07 13:22
 * targetNamespace  命名空间,一般为当前接口包名的倒序
*/

@WebService(targetNamespace="http://service.jpl.com/")
public interface WsService {
   

    @WebMethod(operationName= "say" )
    public String say(String name);

}

 

编写实现类

package com.jpl.service.impl;

import com.jpl.service.WsService;

import javax.jws.WebService;

/**
* @author xla
* @date 2023-01-07 13:25
 * targetNamespace 与当前类实现的接口中的targetNamespace要一致
 * serviceName  对外暴露的服务名
 * endpointInterface  是当前类实现接口类的地址
*/
@WebService(targetNamespace="http://service.jpl.com/",endpointInterface="com.jpl.service.WsService",serviceName = "CrmWs")
public class WsServiceImpl implements WsService {
   
    @Override
    public String say(String name) {
   
        System.out.println(name);
        StringBuffer result = new StringBuffer();
        result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        result.append("<root>");
        result.append("<returnCode>0</returnCode>");
        result.append("<message>调用成功</message>");
        result.append("<name>");
        result.append(name);
        result.append("</name>");
        result.append("</root>");
       return result.toString();
    }
}

 

发布webservice

package com.jpl.config;

import com.jpl.service.WsService;
import com.jpl.service.impl.WsServiceImpl;
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 org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import javax.xml.ws.Endpoint;

/**
* @author xla
* @date 2023-01-07 13:44
 * @deprecated 发布webservice
*/
@Configuration
public class CxfConfig {
   


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

    @Bean(name = "wsBean")
    public ServletRegistrationBean dispatcherServlet() {
   
        ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(),
                "/service/*"); //找到webservice接口中targetNamespace的值,并去掉启动类之前的包名,将剩下来包名填到这里
        return wbsServlet;
    }

    @Bean
    public WsService userService() {
   
        return new WsServiceImpl();
    }

    @Bean
    public Endpoint endpointPurchase(SpringBus springBus, WsService WsService) {
   
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
        endpoint.publish("/CrmWs");
        System.out.println("服务发布成功!地址为:http://localhost:1000/wbse/service/CrmWs?wsdl");
        return endpoint;


    }
}

 

浏览器访问

服务发布成功后,在浏览器中输入地址即可以看到所有的方法

postman调用


请求地址

http://localhost:1000/wbse/service/CrmWs?wsdl

请求体

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <say xmlns="http://service.jpl.com/">
            <arg0 xmlns="">"17364062985"</arg0>
        </say>
    </Body>
</Envelope>


<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <方法名 xmlns="命名空间">
            <arg0 xmlns="">"17364062985"</arg0>
        </方法名>
    </Body>
</Envelope>

 

转载:https://blog.csdn.net/qq_46657663/article/details/128594299
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场