【问题标题】:Spring Integration - Unable to process multipart/form-data with Http-inbound GatewaySpring Integration - 无法使用 Http 入站网关处理多部分/表单数据
【发布时间】:2021-02-04 17:44:53
【问题描述】:

这是一个 Spring Integration(基于 Spring Boot)项目,我已经配置了 Spring Integration 应用程序如下,

配置xml:


<int:channel id="httpChannel"/>

 <int-http:inbound-gateway  supported-methods="GET,POST" request-channel="httpChannel" reply-channel="outputChannel" path="/"
  message-converters="converter" 
 /> 
 <bean id="converter" class="in.tc.springintegrationdemo.CustomConverter" />
    
    
  <int:transformer ref="customTransformer" input-channel="httpChannel" output-channel="outputChannel" />  

  <bean id="customTransformer" class="in.tc.springintegrationdemo.MultipartTransformer" />  
  
   <bean class="in.tc.springintegrationdemo.MessageUpdateBean" id="printMessage"/> 
<int:service-activator ref="printMessage" method="print"  input-channel="outputChannel">
</int:service-activator>

CustomConverter 定义如下


public class CustomConverter implements HttpMessageConverter<MultiValueMap<String,?>> {

    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
      ...
    }

    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return false;
    }

    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return Collections.singletonList(MediaType.MULTIPART_FORM_DATA);
    }

    @Override
    public MultiValueMap<String,?> read(Class<? extends MultiValueMap<String,?>> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {
         MultiValueMap<String, ?> result = new LinkedMultiValueMap<>();
         String str = "";
        MediaType contentType = inputMessage.getHeaders().getContentType();
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(inputMessage.getBody(), contentType.getCharset()));
                while((str=bufferedReader.readLine())!=null) {
                
                    //code to retrieve multipart data
                }
        return result;
    }

    @Override
    public void write(MultiValueMap<String,?> t, MediaType contentType, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        throw new UnsupportedOperationException();
    }

对于多部分/表单数据,我总是在 CustomConverter 类中得到一个空白有效负载。

我也尝试添加一个 multipartResolver,如下所示

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> but it has the same result.

我在 stackoverflow Spring Integration dsl - HTTP inbound gateway consume multipart/form-data 上关注了一个类似的问题。 但对我来说它不起作用。

多部分/表单数据负载

POST / HTTP/1.1
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: d24d89ef-c52b-492a-96b9-6ef6807e8d1a
Host: localhost:9090
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------865732735860935509694157
Content-Length: 217

----------------------------865732735860935509694157
Content-Disposition: form-data; name=""; filename="testfile.txt"
Content-Type: text/plain

testcontent
----------------------------865732735860935509694157--

感谢期待。

【问题讨论】:

    标签: spring spring-boot spring-integration


    【解决方案1】:

    我认为你需要放弃你的CustomConverter,转而使用现有的开箱即用的MultipartAwareFormHttpMessageConverter,它在任何&lt;int-http:inbound-gateway&gt; bean 定义中都默认呈现。

    既然你说你使用 Spring Boot,那么你不需要对 multipartResolver 做任何事情 - 它是在 MultipartAutoConfiguration 中为我们自动配置的。

    如果这仍然不适合您,我很乐意研究您的项目的一个简单而简短的版本,以便使用、复制和可能的修复。

    不相关:有机会了解为什么使用 XML 配置,而不使用 Java 和注释甚至 Java DSL?

    【讨论】:

    • 感谢您的及时回复。我删除了 CustomConverter 并添加了一个 Transformer 来处理 multipart-formdata 但它接收一个空映射作为有效负载的一部分。正如您建议尝试使用 Annotation 和 DSL 一样,我创建了一个基于 DSL 的示例项目来处理 multipart/form-data。但它也无法处理它。我也在这里得到一张空地图。基于 DSL 的项目位于 github.com/bharatguru1983/demo link。提前致谢。
    • 好。明天我会调查的
    • 我该如何测试这个?这是您的项目,所以只有您知道调用什么以及如何调用。您介意分享一些描述如何测试吗?或者甚至更好地添加一个单元测试来确认问题。
    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2015-03-28
    • 2017-07-29
    • 2016-05-07
    • 1970-01-01
    • 2022-07-06
    • 2021-02-20
    相关资源
    最近更新 更多