天天看點

Spring Integration系列《2》 http項目

本案例一共需要兩個項目,一個server項目,一個client項目。

測試截屏

運作client項目的httpdemo main方法,可以收到server端傳回的字元串,并且列印在控制台,就表示項目運作成功。

Spring Integration系列《2》 http項目

server項目

server隻是用來測試用的,提供一個可以通過http請求傳回字元串的接口就可以,不涉及到integration的知識。

本次測試,我簡單的用springboot 寫了一個接口。

  • 源碼位址

    https://gitee.com/bseaworkspace/study_java_web/tree/master/springbootbasic

  • 代碼結構
    Spring Integration系列《2》 http項目
  • 測試
Spring Integration系列《2》 http項目

client項目

采用了spring integration getway的方式,從http的接口中擷取傳回資料。

  • 源碼位址

    https://gitee.com/bseaworkspace/study_java_web/tree/master/springintegrationHttp

  • 代碼結構
    Spring Integration系列《2》 http項目
  • 測試

    運作client項目的httpdemo main方法,可以收到server端傳回的字元串,并且列印在控制台,就表示項目運作成功。

Spring Integration系列《2》 http項目

源碼解析

  • Gateway
Spring Integration系列《2》 http項目

gateway有一個service-interface的屬性,這個屬性指向一個interface (自定義的RequestGateway),這樣,當使用者調用該接口的方法的時候,spring integration就會自動地幫我們建立一個message,并送出到default-request -channel指定地channel上。

package com.xsz;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author Oleg Zhurakousky
 * @author Gary Russell
 *
 */
public class HttpClientDemo {

    private static Log logger = LogFactory.getLog(HttpClientDemo.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
                "/META-INF/spring/integration/http-outbound-config.xml");
        RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class);
        String reply = requestGateway.echo("Hello");
        logger.info("\n\n++++++++++++ Replied with: " + reply + " ++++++++++++\n");
        context.close();
    }

}

           

springintegrationHttp/src/main/resources/META-INF/spring/integration/http-outbound-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-http="http://www.springframework.org/schema/integration/http"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/integration/http https://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

    <int:gateway id="requestGateway"
                 service-inter
                 default-request-channel="requestChannel"/>

    <int:channel id="requestChannel"/>

    <int-http:outbound-gateway request-channel="requestChannel"
                               url="http://localhost:9001/springboot1/server"
                               http-method="POST"
                               expected-response-type="java.lang.String"/>

</beans>
           

Spring Integration HTTP Support

官方文檔位址:

https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#inbound

Spring Integration系列《2》 http項目
  • 本項目往外發送HTTP請求的元件

需要預先設定外部http api傳回的資料類型, 外面api的位址,請求方式等等

Spring Integration系列《2》 http項目
  • 本項目接收外部HTTP請求的元件
    Spring Integration系列《2》 http項目

Spring Integration系列

  • 《1》Hello World項目

    https://blog.csdn.net/h356363/article/details/112076788

  • 《2》 http項目

    https://blog.csdn.net/h356363/article/details/112120991

更多資料關注微信公衆平台

Spring Integration系列《2》 http項目

繼續閱讀