【笔记】springboot+CXF+webservice(soap)+复杂xml2object转换

CXF介绍

Apache CXF = Celtix + XFire,CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或 者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。

CXF 包含了大量的功能特性,但是主要集中在以下几个方面:

1.支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。
2.Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL 优先开发,也支持从 Java 的代码优先开发模式。
3.容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。
支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非 XML 的类型绑定,比如:JSON 和 CORBA。

1、依赖

         <!--webService需要用到的包-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>

2、对外发布的接口

package com.hsit.third.in.soap.service;


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

/**
 * @deprecated 不告诉你叫啥
 * @author sonia
 * @date 2022-12-12
 */
@WebService(targetNamespace = "http://service.soap.in.third.hsit.com")//targetNamespace:命名空间,一般为包名倒序
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface CustomerService {
    @WebMethod
    String syncBDMDataExchange(@WebParam(name = "reqXml") String reqXml);
}

3、接口实现类

package com.hsit.third.in.soap.service.impl;

import com.hsit.third.in.soap.entity.BDMDataExchangeRequestXml;
import com.hsit.third.in.soap.service.CustomerService;

import com.hsit.third.in.utils.XmlUtils;
import lombok.SneakyThrows;
import nonapi.io.github.classgraph.json.JSONSerializer;

import javax.jws.WebService;

/**
 * @deprecated 不告诉你叫啥
 * @author sonia
 * @date 2022-12-12
 */
//serviceName:要发布的服务名,targetNamespace:命名空间,一般为包名倒序,endpointInterface:接口路径
@WebService(serviceName = "CustomerService",targetNamespace = "http://service.soap.in.third.hsit.com", endpointInterface = "com.hsit.third.in.soap.service.CustomerService")
public class CustomerServiceImpl implements CustomerService {

    @SneakyThrows
    @Override
    public String syncBDMDataExchange(String reqXml) {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<bdmdataexchange>" +
                "<head>" +
                "<msgid>E127000000001</msgid>" +
                "<msgcode>20060704001</msgcode>" +
                "<msgtype>12345678</msgtype>" +
                "<sourcesys>BOC</sourcesys>" +
                "<targetsys>b2e0001</targetsys>" +
                "<createtime>b2e0001</createtime>" +
                "</head>" +
                "<body>" +
                "<row action=\"I\">" +
                "<base>" +
                "<apple_type>123</apple_type>" +
                "</base>" +
                "</row>" +
                "<row>" +
                "<base>" +
                "<apple_type>456</apple_type>" +
                "</base>" +
                "</row>" +
                "</body>" +
                "</bdmdataexchange>";
        BDMDataExchangeRequestXml requestXml = (BDMDataExchangeRequestXml) XmlUtils.xmlStrToObject(BDMDataExchangeRequestXml.class, xmlStr2);
        String result_xmlStr = XmlUtils.objectToXmlStr(requestXml, BDMDataExchangeRequestXml.class);
        return JSONSerializer.serializeObject(result_xmlStr);
    }
}

tips:接口类和实现类上注解的targetNamespace属性必须要一样,否则在远程调用的时候会出现以下报错.

 4、配置类

package com.hsit.third.in.soap.config;

import javax.xml.ws.Endpoint;

import com.hsit.third.in.soap.service.CustomerService;
import com.hsit.third.in.soap.service.impl.*;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
//import org.apache.cxf.endpoint.EndpointImpl;
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;

/**
 * @deprecated 配置类
 * @author sonia
 * @date 2022-12-12
 */
@Configuration
public class AppConfig {
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/service/*");
    }

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

    @Bean
    public CustomerService customerService() {
        return new CustomerServiceImpl();
    }

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

浏览器访问:

http://localhost/yxcloud/3rd/in/service/customer?wsdl

soapUI请求:


版权声明:本文为qq_43201836原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。