SpringBoot发布web service接口,并使用ABAP调用web service<转载>

这篇具有很好参考价值的文章主要介绍了SpringBoot发布web service接口,并使用ABAP调用web service<转载>。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

原文链接:https://www.cnblogs.com/ybinlog/p/13962634.html
1、引入依赖

    <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>

关于cxf的版本,要针对不同的spring boot版本选择对应的版本,否则可能会导致容器启动失败:

spring boot的1.5.6.RELEASE版本,可选择cxf的3.1.11版本;

spring boot的2.1.3.RELEASE版本,可选择cxf的3.2.5版本;

2、编写web service接口类:CommonService.java

package com.ybin.webservice.webservice;

import com.ybin.webservice.beans.AjaxResult;
import com.ybin.webservice.po.MaterialInfo;
import com.ybin.webservice.po.SapSecurityInfo;

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

/**
 * @Author: bingy
 * @Date: 2020.11.9 23:11
 * @Version: 1.0
 * @Description:
 **/

@WebService(name = "CommonService",           // 暴露服务名称
        targetNamespace = "http://fabric.ribo.com/" // 命名空间,一般是接口的包名倒序
)
public interface CommonService {
    /**
     * 测试web service
     * @param name
     * @return
     */
    @WebMethod
    @WebResult(name = "String", targetNamespace = "")
    public String sayHello(@WebParam(name = "userName") String name);

    /**
     * 获取sap物料信息
     * @param sapSecurityInfo
     * @param materialInfo
     * @return
     */
    @WebMethod(action = "putMaterialData")
    public @WebResult
    AjaxResult putMaterialData(
            @WebParam(name = "sapSecurityInfo") SapSecurityInfo sapSecurityInfo,
            @WebParam(name = "materialInfo") MaterialInfo materialInfo
    );
}

3、编写web service实现类:CommonServiceImpl.java

package com.ybin.webservice.webservice;

import com.alibaba.fastjson.JSONObject;
import com.ybin.webservice.beans.AjaxResult;
import com.ybin.webservice.po.MaterialInfo;
import com.ybin.webservice.po.SapSecurityInfo;
import com.ybin.webservice.service.MaterialService;
import com.ybin.webservice.util.LogUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import javax.xml.ws.BindingType;

/**
 * @Author: bingy
 * @Date: 2020.11.9 23:27
 * @Version: 1.0
 * @Description:
 **/

@WebService(serviceName = "CommonService",                       // 与接口中指定的name一致
        targetNamespace = "http://webservice.ybin.com/",             // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.ybin.webservice.webservice.CommonService"// 接口地址
)
@BindingType(value = "http://www.w3.org/2003/05/soap/bindings/HTTP/")
@Component
public class CommonServiceImp implements CommonService {
    Logger logger = LogUtils.getBussinessLogger();
    @Autowired
    MaterialService materialService;

    @Override
    public String sayHello(String name) {
        return "Hello , " + name;
    }

    @Override
    public AjaxResult putMaterialData(SapSecurityInfo sapSecurityInfo, MaterialInfo materialInfo) {
        logger.info("sapSecurityInfo->" + JSONObject.toJSONString(sapSecurityInfo));
        logger.info("materialInfo->" + JSONObject.toJSONString(materialInfo));
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = materialService.saveFromWebService(sapSecurityInfo, materialInfo.getMaterialHeader());
        } catch (Exception e) {
            e.printStackTrace();
            String errMsg = e.getMessage() != null ? e.getMessage() : "操作失败";
            ajaxResult.setRetcode(AjaxResult.FAILED);
            ajaxResult.setRetmsg(errMsg);
        }
        logger.info("ajaxResult->" + JSONObject.toJSONString(ajaxResult));
        return ajaxResult;
    }
}

省略业务相关service层代码…

4、编写cxf配置类:CxfConfig.java

package com.ybin.webservice.config;

import com.ybin.webservice.webservice.CommonService;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.Bus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Author: bingy
 * @Date: 2020.11.9 23:45
 * @Version: 1.0
 * @Description:
 **/

@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;
    @Autowired
    CommonService commonService;

    /**
     * JAX-WS
     **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, commonService);
        endpoint.publish("/CommonService");
        return endpoint;
    }
}

5、启动项目,查看wsdl
web service默认发布在 http://localhost:8081/services/ 路径下:
SpringBoot发布web service接口,并使用ABAP调用web service<转载>
6、用soapUI测试没有问题
SpringBoot发布web service接口,并使用ABAP调用web service<转载>
7、用ABAP调用web service

*&---------------------------------------------------------------------*
*& FORM fm_call_http_client
*&---------------------------------------------------------------------*
FORM fm_call_http_client.
  DATA is_zweb_config TYPE zweb_config.
SELECT SINGLE * INTO CORRESPONDING FIELDS OF is_zweb_config FROM zweb_config
    WHERE sysname = 'YBIN_SERVICE'
      AND ifname = '/services/CommonService?'.
  IF is_zweb_config IS INITIAL.
    PERFORM fm_write_log USING '[ERROR]: There is no configurtion about host and port in table [ZWEB_CONFIG]...'.
    RETURN.
  ENDIF.

  soapaction = 'http://' && is_zweb_config-host && ':' && is_zweb_config-port && is_zweb_config-ifname.
  CONDENSE soapaction NO-GAPS.
  PERFORM fm_write_log USING soapaction.


*  测试代码
*  CLEAR soapaction.
*  soapaction = 'http://10.10.76.193:8081/services/CommonService?'.
*  CLEAR lv_xml.
*lv_xml = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://webservice.ybin.com/">'.
*   lv_xml = lv_xml && '<soap:Header/>'.
*   lv_xml = lv_xml && '<soap:Body>'.
*      lv_xml = lv_xml && '<web:putMaterialData>'.
*         lv_xml = lv_xml && '<sapSecurityInfo>'.
*            lv_xml = lv_xml && '<password>EEE</password>'.
*            lv_xml = lv_xml && '<username>EEE</username>'.
*         lv_xml = lv_xml && '</sapSecurityInfo>'.
*         lv_xml = lv_xml && '<materialInfo>'.
*            lv_xml = lv_xml && '<materialHeader>'.
*               lv_xml = lv_xml && '<CLABS>1</CLABS>'.
*               lv_xml = lv_xml && '<EX_SAISJ>2020</EX_SAISJ>'.
*               lv_xml = lv_xml && '<EX_ZFK></EX_ZFK>'.
*               lv_xml = lv_xml && '<EX_ZMLCF></EX_ZMLCF>'.
*               lv_xml = lv_xml && '<EX_ZMLKZ></EX_ZMLKZ>'.
*               lv_xml = lv_xml && '<j_3AKORDX>color001</j_3AKORDX>'.
*               lv_xml = lv_xml && '<MAKTX>123</MAKTX>'.
*               lv_xml = lv_xml && '<MATNR>matnr002</MATNR>'.
*               lv_xml = lv_xml && '<MEINS>KAN</MEINS>'.
*               lv_xml = lv_xml && '<VERPR>12.22</VERPR>'.
*            lv_xml = lv_xml && '</materialHeader>'.
*         lv_xml = lv_xml && '</materialInfo>'.
*      lv_xml = lv_xml && '</web:putMaterialData>'.
*   lv_xml = lv_xml && '</soap:Body>'.
*lv_xml = lv_xml && '</soap:Envelope>'.

*  创建http_client对象
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url    = soapaction
    IMPORTING
      client = http_client.

*  Display Logon Screen = 1
  http_client->propertytype_logon_popup = http_client->co_enabled.

*  设置头信息[name,value]
  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'text/xml;charset="UTF-8"'.

  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'SOAPAction'
      value = soapaction.

  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'User-Agent'
      value = 'compatible; MSIE 5.0;'.

*  将此实体的HTTP主体设置为char
  CALL METHOD http_client->request->set_cdata
    EXPORTING
      data = lv_xml.

*  发送http请求
  CALL METHOD http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.

*  接收http响应
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

*  返回此实体的HTTP正文作为字符数据
  CLEAR lv_log.
  lv_log = http_client->response->get_cdata( ).

  CLEAR xmldata.
  xmldata = http_client->response->get_data( ).

*  关闭http连接
  CALL METHOD http_client->close.

*解析响应返回的数据
  CLEAR lt_return[].
  CALL FUNCTION 'SMUM_XML_PARSE'
    EXPORTING
      xml_input = xmldata
    TABLES
      xml_table = lt_retable
      return    = lt_return.


  PERFORM fm_write_log USING '===== 日志 ======'.
  DATA iv_log TYPE string.
  PERFORM fm_write_log USING '[RETLOG]: Returned log:'.
  WHILE lv_log NE ''.
    SPLIT lv_log AT '>' INTO iv_log lv_log.
    iv_log = iv_log && '>'.
    PERFORM fm_write_log USING iv_log.
  ENDWHILE.
PERFORM fm_write_log USING 'Http Response:'.
  LOOP AT lt_retable.
    CLEAR iv_log.
    iv_log = 'type:' && lt_retable-type && '; name:' && lt_retable-cname && '; value:' && lt_retable-cvalue && ';'.
    PERFORM fm_write_log USING iv_log.
  ENDLOOP.

ENDFORM.

此处有一点,用ABAP调用web service需要在事务代码:SICF 中配置启用web service。

另外附上大佬的另一篇文章:里面包含了JAVA调用RFC的代码,但却不知这些
在spring boot中使用sapjco3,并用docker部署到Linux服务器文章来源地址https://www.toymoban.com/news/detail-406489.html

到了这里,关于SpringBoot发布web service接口,并使用ABAP调用web service<转载>的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C#引用Web Service 类型方法,添加搜索本地服务器Web Service 接口调用方法

    wsdl首先保证现在网络能调用web service接口,右键项目添加服务引用 点击高级 添加web服务 输入搜索的服务器接口,选中你要添加调用的方法即可 添加完成调用方法

    2024年02月13日
    浏览(34)
  • ABAP:调用HTTP接口详解

    DATA:pv_http_client TYPE REF TO if_http_client, pv_url TYPE string. 1.可以通过URL连接或者IP:端口连接,根据实际情况而定, 建议使用URL,省去地址服务器域名转换 \\\"URL接连 CALL METHOD cl_http_client=create_by_url EXPORTING url = pv_url IMPORTING client = pv_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2

    2023年04月12日
    浏览(34)
  • ABAP - 调用HTTP/HTTPS接口上传文件

    如果调用该外部接口是需要使用Oauth2.0的话,可参照该文章里的OAuth2.0的部分: ABAP代码使用OAuth2.0 Client Credential调用外部API 首先,先将文件内容转成binary格式的内表 , 可参照文章: 内表文件的,可参照ABAP - 下载ALV内表 - download ALV internal table SOST里的附件的,可参照ABAP获取发

    2024年02月20日
    浏览(31)
  • 【SAP ABAP】SAP Webservice & RESTful 接口服务发布教程

    关于 WebService 概念,这篇文章讲解的非常全面,可以移步阅读《SAP Web service》。 本想通过 RFC 来发布 ODATA 服务,奈何当前 SAP ECC 版本过低不支持,只好采用其他方式来发布服务,于是就尝试了下面这两种方法。 SE37,创建以下测试用 RFC 测试执行 RFC,得到的数据结果如下图:

    2024年02月02日
    浏览(35)
  • springboot使用restTemplate调用webservice接口

    1.首先确定wsdl的http地址,使用postman测试接口是否成功  在浏览器输入webservice地址可以找到相应的请求和响应示例。    如果postman返回了正确的数据,就说明测试成功! 2.接下来代码:

    2024年01月16日
    浏览(52)
  • SpringBoot 使用RestTemplate来调用https接口

    使用RestTemplate来访问第三方https接口,接口类型为POST,返回数据为JSON,需要忽略https证书,因此对RestTemplate 进行配置,通过HttpClient的请求工厂(HttpComponentsClientHttpRequestFactory)进行构建。HttpComponentsClientHttpRequestFactory使用一个信任所有站点的HttpClient就可以解决问题了。 中间

    2024年02月13日
    浏览(41)
  • 【新手向】如何使用postman调用springboot接口

    springboot:2.7.2 mybatis:3.5.11 postgresql:42.3.6 jdk:8 项目启动之后,无法使用postman调用本地接口。返回no message available: 调用方式为Post。调整postman的header,新增Content-Type为application/json 为方法入参加上@RequestBody注解 Rest接口与启动类不在同一包下,新增@ComponentScan注解,指定扫描

    2024年01月16日
    浏览(29)
  • 对接 Web Service第三方接口

    这次也是头一次接触对接第三方WebService接口,这技术都很老了,使用postman测试的时候还找了半天资料🤣。 一般来说第三方都会限制ip这些,需要注意的是,给到的接口地址是能用公网进行访问的哦。 1、拿到接口路径 http://111.111.11.1:111/services/infoWebService?wsdl 这个当然是不可

    2023年04月11日
    浏览(36)
  • SpringBoot 整合Thymeleaf教程及使用 springboot配合thymeleaf,调用接口不跳转页面只显示文本

    Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。它与 JSP,Velocity,FreeMaker 等模板引擎类似,也可以轻易地与 Spring MVC 等 Web 框架集成。与其它模板引擎相比,Thymeleaf 最大的特点是,即使不启动 Web 应用,也可以直接在浏览器中打开并正确显示模板页面 。 目录 一、整合

    2024年02月08日
    浏览(66)
  • SpringBoot项目中添加了@Service然而无法注入Service接口的问题

    前言 昨天在项目中使用代码生成器生成了各层面的代码,但是由于未知的原因一直无法调用。经过多方查找后才发现是@MapperScan注解的问题,由于这个藏得比较隐蔽,所以在此记录一下。 问题描述 在接口完成后调用接口,发现无法调用接口,显示错误是 大致的意思是绑定错

    2024年02月02日
    浏览(72)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包