spring 集成的多部分请求未按预期工作

multipart request not working as expected for spring integration

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

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


第 1 步使用 MultiValueMap 按预期工作,而当我尝试使用相同的过程将文档字节发送到服务 B 时 在第 2 步中 - 我得到 Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [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;

}

Spring 整合 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 整合 xml 来完成。 我正在使用 spring boot 2.0.2 BOM。

我认为问题是在 expected-response-type="[B" 的第一个请求之后你得到一个 contetType header 作为 application/octet-stream 而这不适合第二个在你有 MultiValueMap 的地方请求,但没有任何钩子如何表示它。

我建议你在发送第二个请求之前添加一个header-enricher:

<int:service-activator
        input-channel="sourceDocumentOutLv1"
        ref="docConversionOrchestratorImpl" method="processRest1AndSendToRest2"
        output-channel="enrichContentTypeHeaderChannel" />

<int:header-enricher input-channel="enrichContentTypeHeaderChannel" output-channel="sourceDocumentOutLv2">
    <int:header name="contentType" value="multipart/form-data" overwrite="true"/>
</int:header-enricher>