zoukankan      html  css  js  c++  java
  • Springboot整合webservice

    2019-12-10 16:34:42 星期二

    WebService是什么

    WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的相互调用与开发语言无关

    WebService平台技术

    • XML+XSD

    WebService采用HTTP协议传输数据,采用XML格式封装数据

    • SOAP

    WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用Web Service。

    • WSDL

    基于XML的语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都能理解的标准格式

    springboot整合WebService

    1、Springboot中已经有配置的webservice的jar包,我们在开发时直接引入即可

    <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
       <version>3.2.6</version>
    </dependency>
    

    项目中的Springboot父版本采用2.2.0

    2、webservice服务接口

    @Service
    @WebService(name = "MasterDataService",     //该名字可自定义
            targetNamespace = "http://webservice/system.biz.medical.com"    // 该URL一般为当前包名的倒序
    )
    public interface EmrWebService {
    
        /**
         * 服务调用
         *
         * @param data String
         * @return String
         */
        @WebMethod
        String emrService(@WebParam String data);
    
    }
    

    3、接口实现

    @Slf4j
    @Service
    @WebService(name = "MasterDataService",     // 与接口中的name相同
            targetNamespace = "http://webservice/system.biz.medical.com",   // 一般为当前包名的倒序
            endpointInterface = "com.medical.biz.system.webservice.EmrWebService"   // 为接口类的包名
    )
    public class EmrWebServiceImpl implements EmrWebService {
    
        private static final String RESPONSE = "<Response><Header><SourceSystem>%s</SourceSystem><MessageID>%s</MessageID></Header><Body><ResultCode>%s</ResultCode><ResultContent>%s</ResultContent></Body></Response>";
    
        @Override
        public String emrService(@WebParam String data) {
            log.info("接收参数 => [ {} ]", data);
            if (data.isEmpty()) {
                return "传入的参数为空";
            }
    
            return String.format(RESPONSE, "01", "", "0", "成功");
        }
    }
    

    4、配置cxf服务发布

    注意下方代码中注释信息中的坑点

    @Configuration
    public class CxfConfig {
    
        private Bus bus;
        private EmrWebService emrWebService;
    
        @Autowired
        public CxfConfig(Bus bus, EmrWebService emrWebService) {
            this.bus = bus;
            this.emrWebService = emrWebService;
        }
    
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(bus, emrWebService);
            endpoint.publish("/MasterDataService");
            return endpoint;
        }
    
        /**
         * 坑点:
         *  1、方法名为dispatchServlet()
         *      如果Springboot的主版本在2.0.X以下时,可以正常启动,此时在方法中配置的访问路径将会覆盖默认或者在application.properties文件中配置server.servlet.context-path=中的值
         *      如果Springboot的主版本在2.0.X以上时,此时启动报错,不能正常启动
         *          此时需要将方法名更改,不能用dispatchServlet(),在方法中配置webservice的访问路径,不会与项目配置的全局访问路径冲突,
         *
         * @return ServletRegistrationBean
         */
        @SuppressWarnings("all")
        @Bean
        public ServletRegistrationBean disServlet() {
            // 此处配置的是webservice接口的访问地址,类似 http://127.0.0.1:8001/emr
            return new ServletRegistrationBean(new CXFServlet(), "/emr/*");
        }
    }
    

    5、启动项目,访问

    http://127.0.0.1:8001/emr,可以看到服务信息

    注册的服务信息

    推荐使用soapUI进行服务测试,结果如下:

    webservice服务测试

  • 相关阅读:
    EntityFramework 启用迁移 EnableMigrations 报异常 "No context type was found in the assembly"
    JAVA 访问FTP服务器示例(2)
    NuGet Package Manager 更新错误解决办法
    JAVA 访问FTP服务器示例(1)
    RemoteAttribute 的使用问题
    诡异的 javascript 变量
    javascript apply用法
    Babun 中文乱码
    GSM呼叫过程
    转站博客园
  • 原文地址:https://www.cnblogs.com/reroyalup/p/12017610.html
Copyright © 2011-2022 走看看