我有两个休息服务和一个听众
- 服务A
- 服务乙
- 监听器 L1
- 第 1 步 - 侦听器 L1 从本地读取文件并将多值映射发送到服务 A。服务 A 从数据库中检索一些文档并将其作为字节返回给侦听器 L1。
第 2 步 - 侦听器 L1 然后将另一个多值映射发送到服务 B 并保存文档。
步骤 1 使用 MultiValueMap 按预期工作,当我尝试使用相同的过程将文档字节发送到服务 B 在步骤 2 中 - 我收到无法写入请求:没有为请求类型找到合适的 HttpMessageConverter [org.springframework. util.LinkedMultiValueMap] 和内容类型 [application/octet-stream]。我正在遵循相同的程序,但仍然遇到问题。
请在下面的代码示例中找到并告诉我如何解决此问题。
Listener1.java
public Message<?> processJMSReqMsqAndSendToRest1(String message) throws Exception {
MultiValueMap<String, Object> mainMap = new LinkedMultiValueMap<String, Object>();
Map<String, String> secondaryMap = new HashMap<String, String>();
secondaryMap.put("key1", "value1");
secondaryMap.put("key2", "value2");
secondaryMap.put("key3", "value3");
byte[] messageBytes = message.getBytes();
File newFile = new File("D:\\Temp\\temp.jpg");
InputStream is = new FileInputStream(newFile);
byte[] fileBytes = IOUtils.toByteArray(is);
is.close();
mainMap.add("metaData", secondaryMap);
mainMap.add("messageBytes", messageBytes );
Message<?> message1 = MessageBuilder.withPayload(mainMap).build();
return message1;
}
public Message<?> processRest1AndSendToRest2(Message<?> obj) throws Exception{
byte[] docBytes = (byte[])obj.getPayload();
MultiValueMap<String, Object> mainMap = new LinkedMultiValueMap<String, Object>();
Map<String, String> secondaryMap = new HashMap<String, String>();
secondaryMap.put("key1", "value1");
secondaryMap.put("key2", "value2");
secondaryMap.put("key3", "value3");
mainMap.add("metaData", secondaryMap);
mainMap.add("messageBytes", docBytes);
Message<?> message1 = MessageBuilder.withPayload(mainMap).build();
return message1;
}
弹簧集成 xml
<int-http:outbound-gateway
id="docServiceOutBoundGateway" request-channel="docMetaDataIn"
http-method="POST" url="http://localhost:8030/getDocument"
expected-response-type="[B" reply-channel="sourceDocumentOutLv1">
</int-http:outbound-gateway>
<int:service-activator
input-channel="sourceDocumentOutLv1"
ref="docConversionOrchestratorImpl" method="processRest1AndSendToRest2"
output-channel="sourceDocumentOutLv2" />
<int-http:outbound-gateway request-channel="sourceDocumentOutLv2"
http-method="POST" url="http://localhost:8030/sendDocument"
encode-uri="false"
expected-response-type="java.lang.String" reply-channel="processedDocOutLv1">
</int-http:outbound-gateway>
服务一:
@RequestMapping(value = "/getDocument", method = RequestMethod.POST)
@ResponseBody
public byte[] testRest1(@RequestPart("metaData")Map<String,String> metaData,@RequestPart("messageBytes")byte[] messageBytes) {
byte[] r2 = //get doc from database as bytes
return r2;
}
服务乙:
@RequestMapping(value = "/sendDocument", method = RequestMethod.POST)
@ResponseBody
public String tesMySql1(@RequestPart("metaData")Map<String,String> metaData,@RequestPart("messageBytes")byte[] messageBytes) {
return "working";
}
我试过通过java直接通过rest模板发送它,效果很好。但我希望结构保持一致,并通过 spring 集成 xml 来完成。我正在使用 spring boot 2.0.2 BOM。