Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Spring Integration - Unable to process multipart/form-data with Http-inbound Gateway

It is a Spring Integration (based on Spring Boot) project, I've configured Spring Integration application as below,

Configuration 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 defined as below


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();
    }

for multipart/form-data, I'm always getting a blank payload in CustomConverter class.

I've also tried adding a multipartResolver as below

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

I've followed a similar question on stackoverflow Spring Integration dsl - HTTP inbound gateway consume multipart/form-data. But for me it's not working.

Multipart/form-data payload

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

Thanks in anticipation.

question from:https://stackoverflow.com/questions/66051029/spring-integration-unable-to-process-multipart-form-data-with-http-inbound-gat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you need to ditch your CustomConverter in favor of existing one out-of-the-box - MultipartAwareFormHttpMessageConverter, which is really presented by default in any <int-http:inbound-gateway> bean definition.

Since you say that you use Spring Boot then you don't need to do anything with the multipartResolver - it is auto-configured for us out-of-the-box in the MultipartAutoConfiguration.

If that still doesn't work for you, I'd be glad to look into a simple and short version of your project to play with, reproduce and possible fix.

Not related: any chances to understand why do use XML configuration, but not Java & Annotations or even Java DSL?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...