多部分请求无法像Spring集成那样正常工作

时间:2018-10-03 14:39:26

标签: java spring-integration spring-integration-http

我有两个休息服务和一个监听器

  1. 服务A
    1. 服务B
    2. 侦听器L1

  • 步骤1-侦听器L1从本地读取文件并发送多值 映射到服务A。服务A从数据库中检索一些文档,然后 将其作为字节返回给侦听器L1。
    步骤2-侦听器L1然后发送 另一个多值映射到服务B并保存文档。

第1步使用MultiValueMap可以按预期工作,就像我尝试使用相同过程将文档字节发送到服务B一样 在第2步- 我收到无法写入请求:找不到适合请求类型[org.springframework.util.LinkedMultiValueMap]和内容类型[application / octet-stream] 的HttpMessageConverter。我正在执行相同的步骤,但仍然遇到问题。

请在下面的代码示例中查找,并让我知道如何解决此问题。

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;

}

Spring Integration 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>

服务A:

@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;
    }

服务B:

@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 Integration xml完成​​。 我正在使用Spring Boot 2.0.2 BOM。

1 个答案:

答案 0 :(得分:1)

我认为这样的问题是,在第一个使用expected-response-type="[B"的请求之后,您会得到一个contetType的{​​{1}}标头,而这不适用于您有{ {1}},但是没有任何钩子来表示它。

我建议您在发送第二个请求之前添加application/octet-stream

MultiValueMap