首页 > 其他分享 >springboot 整合 webservice服务

springboot 整合 webservice服务

时间:2023-04-13 15:33:41浏览次数:40  
标签:springboot service result 整合 org import public webservice

目录


在前一段时间用到了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;


    }
}

浏览器访问

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

postman调用

springboot 整合 webservice服务_spring_02
请求地址

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>

标签:springboot,service,result,整合,org,import,public,webservice
From: https://blog.51cto.com/u_15314615/6188100

相关文章

  • 如何实现 Java SpringBoot 自动验证入参数据的有效性
    JavaSpringBoot通过javax.validation.constraints下的注解,实现入参数据自动验证如果碰到@NotEmpty否则不生效,注意看下@RequestBody前面是否加上了@ValidValidation常用注解汇总Constraint详细信息@Null被注释的元素必须为null@NotNull被注释的元素必须不......
  • SpringBoot尚硅谷
    SpringBoot2核心技术与响应式编程SpringBoot2核心技术SpringBoot2基础入门Spring能做什么?Spring的生态覆盖了:web开发数据访问安全控制分布式消息服务移动开发批处理Spring5的重大升级内部源码设计基于Java8的一些新特性,如:接口默认实现。重新设计源码......
  • 基于Java+Springboot+vue网上商品订单转手系统设计和实现
    基于Java+Springboot+vue网上商品订单转手系统设计和实现一、前言介绍:1.1项目摘要传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装网上商品订单转手系统软件来发挥其高效地信息处理......
  • springboot学习随笔
    1.大纲-springboot框架1.什么是Springboot以及Springboot的特点。2.快速搭建springboot项目3.springboot常用的配置文件类型.4.读取springboot配置文件的内容5.多环境配置6.springboot整合数据源。7.springboot整合mybatis.8.springboot整合定时器。2.Springbo......
  • SpringBoot向web容器注入Servlet,Filter及SpringSecurity注册DelegatingFilterProxy
    从SpringSecurity架构图可知SpringSecurity的过滤器与Web容器的过滤器是通过DelegatingFilterProxy接入的。由DelegatingFilterProxy代理了FilterChainProxy,FilterChainProxy包含了SpringSecurity的过滤器链。 那么DelegatingFilterProxy是怎么创建及如何加入到Web容器中? 看......
  • Springboot三种启动方式
    在https://start.spring.io/上创建一个springboot工程生成的代码中的启动方式咱们暂时定义为默认方式:/***@auther:lawt*@date:2018/12/117*@Description:默认启动方式*/@SpringBootApplicationpublicclassMicroServicesSpringBootApplication{publicstaticv......
  • springboot 中的 classpath 指的是什么路径?
    classpath其本质其实是指项目打包后的classes下的路径,一般用来指代“src/main/resources”下的资源路径。通常会在各种配置文件中使用【classpath】关键字,例如:yml配置文件:WebMvcConfigurer配置类:......
  • springboot整合阿里云OSS实现多线程下文件上传(aop限制文件大小和类型)
    内容涉及:springboot整合阿里云oss自定义注解及aop的使用:对上传文件格式(视频格式、图片格式)、不同类型文件进行大小限制(视频和图片各自自定义大小)线程池使用:阿里云OSS多线程上传文件阿里云OSS分片上传大文件 业务需求需求一:前端传递单个或多个小文件(这里......
  • springboot filter 和 interceptor 使用
    userholderpublicclassUserHolder{privatestaticThreadLocal<String>userThreadLocal=newThreadLocal<>();//为当前的线程变量赋值上用户信息publicstaticvoidsetLoginUser(Stringuser){userThreadLocal.set(user);}//从当前......
  • SpringBoot启动后获取特定注解的Bean实例代码(转)
    来自:https://zhuanlan.zhihu.com/p/375973197本文研究的主要是Spring启动后获取所有拥有特定注解的Bean,具体如下。最近项目中遇到一个业务场景,就是在Spring容器启动后获取所有的Bean中实现了一个特定接口的对象,第一个想到的是ApplicationContextAware,在setApplicationContext中......