Spring Integration系列《2》 http项目

本案例一共需要两个项目,一个server项目,一个client项目。

测试截屏

运行client项目的httpdemo main方法,可以收到server端返回的字符串,并且打印在控制台,就表示项目运行成功。

Spring Integration系列《2》 http项目_第1张图片

server项目

server只是用来测试用的,提供一个可以通过http请求返回字符串的接口就可以,不涉及到integration的知识。
本次测试,我简单的用springboot 写了一个接口。

  • 源码地址
    https://gitee.com/bseaworkspace/study_java_web/tree/master/springbootbasic

  • 代码结构
    在这里插入图片描述

  • 测试

Spring Integration系列《2》 http项目_第2张图片

client项目

采用了spring integration getway的方式,从http的接口中获取返回数据。

  • 源码地址
    https://gitee.com/bseaworkspace/study_java_web/tree/master/springintegrationHttp

  • 代码结构
    Spring Integration系列《2》 http项目_第3张图片

  • 测试
    运行client项目的httpdemo main方法,可以收到server端返回的字符串,并且打印在控制台,就表示项目运行成功。

Spring Integration系列《2》 http项目_第4张图片

源码解析

  • Gateway

Spring Integration系列《2》 http项目_第5张图片

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




    

    

    


Spring Integration HTTP Support

官方文档地址:
https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#inbound

在这里插入图片描述

  • 本项目往外发送HTTP请求的组件

需要预先设置外部http api返回的数据类型, 外面api的地址,请求方式等等
Spring Integration系列《2》 http项目_第6张图片

  • 本项目接收外部HTTP请求的组件
    Spring Integration系列《2》 http项目_第7张图片

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项目_第8张图片

你可能感兴趣的:(Spring,Integration)