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服務測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM