首页 > 解决方案 > 多部分请求无法按预期进行弹簧集成

问题描述

我有两个休息服务和一个听众

  1. 服务A
    1. 服务乙
    2. 监听器 L1


步骤 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。

标签: javaspring-integrationspring-integration-http

解决方案


我认为在第一个请求之后expected-response-type="[B"你会得到一个contetType标头作为 a 的问题application/octet-stream,这不适合你有 a 的第二个请求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>

推荐阅读