网站首页 > 精选教程 正文
在一些简单的应用中,可以用spring-integration来获取http请求内容,从而实现简单的http接口调用,相对于HttpClient要更简单一些,个人理解可以在一些简单的场景下使用,首先需要引入spring-integration-http
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-http</artifactId>
<version>5.3.1.RELEASE</version>
</dependency>
http的配置与其它的spring-integration配置有所区别,这里的入站管道实际上是http请求外网服务器,这里会立即返回数据,而不是像tcp或mqtt那样异步调用数据处理接口,配置如下:
package org.noka.serialservice.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler;
import org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.messaging.MessageHandler;
/** ----------------------------------------------
* http请求配置
* @author xiefangjian@163.com
* @version 1.0.0
** -------------------------------------------**/
@EnableIntegration
@Configuration
public class HttpConfig {
private String url="http://192.168.50.46:8088/";//http服务器地址
/**
* http请求配置
* 这量数据管道名称为 http_out
* @return
*/
@Bean
@ServiceActivator(inputChannel = "http_out")
public MessageHandler httpHandler(){
AbstractHttpRequestExecutingMessageHandler http = new HttpRequestExecutingMessageHandler(url);
DefaultHttpHeaderMapper httpHeaderMapper= new DefaultHttpHeaderMapper();
httpHeaderMapper.setOutboundHeaderNames("*");//允许自定义协议的头部信息
http.setHttpMethod(HttpMethod.POST);//post方式
http.setCharset("UTF-8");//编码方式
http.setHeaderMapper(httpHeaderMapper);
http.setExpectedResponseType(String.class);//请求以纯字符串的方式返回
return http;
}
}
需要请求一个http服务时,同样是通过一个接口来完成,这个接口实现方式如下:
package org.noka.serialservice.service;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import java.util.Map;
/**----------------------------------------------------------------
* 发送消息网关,其它需要发向服务器发送消息时,调用该接口
* @author xiefangjian@163.com
* @version 1.0.0
**--------------------------------------------------------------**/
@MessagingGateway
@Component
public interface MsgGateway {
/**
* http请求接口
* @param out 需要发送的内容
* @return
*/
@Gateway(requestChannel = "http_out")
String Post(Message<Map<String,String>> out);
}
后面在任何地方都可以使用MsgGateway接口来请求服务器的内容,像下面这样
//-----需要传到服务器上的参数----------------------------------------
Map<String,String> pars = new HashMap<>();
pars.put("par1","xiefangjian");//参数1
Message<Map<String,String>> ms = MessageBuilder.withPayload(pars)
.setHeader(HttpHeaders.ACCEPT_LANGUAGE,"zh-CN")//Accept-Language 设置为zh-CN
.setHeader("aaaa","xiefangjian")//自定义协议头
.build();
String body = msgGateway.Post(ms);//这里body为服务器返回的内容
- 上一篇: java-OkHttp使用教程
- 下一篇: 一招搞定外部请求,这款 HTTP 客户端框架真的很强大!
猜你喜欢
- 2024-12-06 使用jmeter进行接口性能测试入门
- 2024-12-06 HttpClient使用不当,服务挂了!是时候系统学习一下了
- 2024-12-06 一次完整的HTTP请求与响应涉及了哪些知识?
- 2024-12-06 HTTP 和 RPC 的区别
- 2024-12-06 基础篇-SpringBoot HTTP接口实战
- 2024-12-06 Spring 框架里的 HTTP 调用,RestTemplate 还是 WebClient
- 2024-12-06 干掉OpenFeign,SpringBoot 3.0 自带的 HTTP 客户端真香!
- 2024-12-06 Springboot -- 用更优雅的方式发HTTP请求(RestTemplate详解)
- 2024-12-06 Feign : 优雅的调用 API
- 2024-12-06 基于Java的开发框架,编写接口通过UI界面完成自动映射成http接口
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- nginx反向代理 (57)
- nginx日志 (56)
- nginx限制ip访问 (62)
- mac安装nginx (55)
- java和mysql (59)
- java中final (62)
- win10安装java (72)
- java启动参数 (64)
- java链表反转 (64)
- 字符串反转java (72)
- java逻辑运算符 (59)
- java 请求url (65)
- java信号量 (57)
- java定义枚举 (59)
- java字符串压缩 (56)
- java中的反射 (59)
- java 三维数组 (55)
- java插入排序 (68)
- java线程的状态 (62)
- java异步调用 (55)
- java中的异常处理 (62)
- java锁机制 (54)
- java静态内部类 (55)
- java怎么添加图片 (60)
- java 权限框架 (55)
本文暂时没有评论,来添加一个吧(●'◡'●)