SpringCloudStream-内容类型和转换

更新时间:2023-05-28 08:40:56 阅读: 评论:0

SpringCloudStream-内容类型和转换
内容类型和转换
要允许您传播关于已⽣成消息的内容类型的信息,默认情况下,Spring Cloud Stream附加contentType标头到出站消息。对于不直接⽀持头⽂件的中间件,Spring Cloud Stream提供了⾃⼰的⾃动将邮件包裹在⾃⼰的信封中的机制。对于⽀持头⽂件的中间件,Spring Cloud Stream应⽤程序可以从⾮Spring Cloud Stream应⽤程序接收具有给定内容类型的消息。
Spring Cloud Stream可以通过两种⽅式处理基于此信息的消息:
通过其⼊站和出站渠道的contentType设置
通过对@StreamListener注释的⽅法执⾏的参数映射
Spring Cloud Stream允许您使⽤绑定的spring.cloud.stream.bindings.<channelName>.content-type属性声明性地配置输⼊和输出的类型转换。请注意,⼀般类型转换也可以通过在应⽤程序中使⽤变压器轻松实现。⽬前,Spring Cloud Stream本机⽀持流中常⽤的以下类型转换:
来⾃/从POJO的JSON
JSON /从
tiger对象到/来⾃byte []:⽤于远程传输的原始字节序列化,应⽤程序发出的字节,或使⽤Java序列化转换为字节(要求对象为
Serializable)
字符串到/来⾃byte []
对象到纯⽂本(调⽤对象的toString()⽅法)
其中JSON表⽰包含JSON的字节数组或字符串有效负载。⽬前,对象可以从JSON字节数组或字符串转换。转换为JSON总是产⽣⼀个String。
如果在出站通道上没有设置content-type属性,则Spring Cloud Stream将使⽤基于序列化框架的序列化程序对有效负载进⾏序列化。在⽬的地反序列化消息需要在接收者的类路径上存在有效载荷类。
MIME类型
content-type值被解析为媒体类型,例如application/json或text/plain;chart=UTF-8。MIME类型对于
指⽰如何转换为String或byte []内容特别有⽤。Spring Cloud Stream还使⽤MIME类型格式来表⽰Java类型,使⽤具有type参数的⼀般类型application/x-java-object。例如,application/x-java-object;type=java.util.Map或application/x-java-object;type=com.bar.Foo可以设置为输⼊绑定的content-type属性。此外,Spring Cloud Stream提供⾃定义MIME类型,特别是application/x-spring-tuple来指定元组。
在Spring Cloud Stream中默认提供了⼀些可以开箱即⽤的类型转换器,具体如下表所⽰:
来源有效载荷⽬标有效载荷content-type标题(来源讯
息)
content-type标题(转换
后)
注释
POJO JSON String ignored application/json
Tuple JSON String ignored application/json JSON是为Tuple量⾝定制的
POJO String
(toString())
ignored text/plain, java.lang.String
POJO byte[]
(rialized)
ignored新概念美音版
application/x-java-rialized-slutty asshole
object
JSON byte[] or
String POJO application/json (or none)application/x-java-object
byte[] or String Serializable application/x-java-rialized-项目管理师
asshole什么意思objectwsp
climateapplication/x-java-object
JSON byte[] or
exceptTuple application/json (or none)application/x-spring-tuple
String
Tuple application/json (or none)application/x-spring-tuple
byte[]String any text/plain, java.lang.String 将应⽤在content-type头中指定的任何Chart
String byte[]any application/octet-stream 将应⽤在content-type头中指定的任何Chart
来源有效载荷⽬标有效载荷content-type标题(来源讯
息)belle是什么意思
joe somebodycontent-type标题(转换
后)
注释

本文发布于:2023-05-28 08:40:56,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/798391.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:类型   字节   序列化   转换
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图