Spring Integration dsl - HTTP 入站网关使用 multipart/form-data [英] Spring Integration dsl - HTTP inbound gateway consume multipart/form-data

查看:24
本文介绍了Spring Integration dsl - HTTP 入站网关使用 multipart/form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 http 入站网关使用 multipart/form-data.第一部分是作为 json 的对象,我想通过内容类型将其序列化为对象:application/json.第二部分是pdf文件.是否可以简单地序列化?我需要我的自定义消息转换器吗?

I would like to consume multipart/form-data through http inboud gateway. First part is object as json which I want to serialize it to object by content-type: application/json. And second part is pdf file. Is it possible to simply serialize? Do I need my custom message converter?

RAW 请求:

POST http://localhost:18080/api/v2/attachments HTTP/1.1
Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
Host: localhost:18080
Content-Length: 542

---------------------------acebdf13572468
Content-Disposition: form-data; name="metadata"
Content-Type: application/json

{
  "version" : "1.0",
}

---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="test.pdf"
Content-Type: application/pdf

<<pdf data>>
---------------------------acebdf13572468--

HTTP 入站网关

@Bean
public IntegrationFlow multipartForm() {
    return IntegrationFlows.from(Http.inboundGateway("/api/v2/attachments")
            .errorChannel("rest.general.error")
            .requestMapping(m -> m
                    .methods(HttpMethod.POST)
                    .consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
            ))
            .channel("requestAttachment.input")
            .get();
}

我已经在 spring boot 2.0.3.RELEASE 上试过了.它似乎不适用于 org.springframework.web.multipart.commons.CommonsMultipartResolver.在有效负载中,我总是看到空的 LinkedMultiValueMap.但我注意到它在默认配置 org.springframework.web.multipart.support.StandardServletMultipartResolver 下正常工作.CommonsMultipartResolver 和 StandardServletMultipartResolver 有什么区别?

I have tried it on spring boot 2.0.3.RELEASE. It seems that it is not working with org.springframework.web.multipart.commons.CommonsMultipartResolver. In the payload I always see empty LinkedMultiValueMap. But I noticed that it is working correctly with default configuration org.springframework.web.multipart.support.StandardServletMultipartResolver. What is difference between CommonsMultipartResolver and StandardServletMultipartResolver?

依赖项

+- org.springframework.boot:spring-boot-starter-web:jar:2.0.3.RELEASE:compile
|  +- org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:compile
|  |  +- org.springframework.boot:spring-boot:jar:2.0.3.RELEASE:compile
|  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.3.RELEASE:compile
|  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.3.RELEASE:compile
|  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
|  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
|  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
|  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
|  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
|  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
|  |  \- org.yaml:snakeyaml:jar:1.19:runtime
|  +- org.springframework.boot:spring-boot-starter-json:jar:2.0.3.RELEASE:compile
|  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.6:compile
|  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
|  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.6:compile
|  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.6:compile
|  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.6:compile
|  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.6:compile
|  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.3.RELEASE:compile
|  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.31:compile
|  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.31:compile
|  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.31:compile
|  +- org.hibernate.validator:hibernate-validator:jar:6.0.10.Final:compile
|  |  +- javax.validation:validation-api:jar:2.0.1.Final:compile
|  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
|  |  \- com.fasterxml:classmate:jar:1.3.4:compile
|  +- org.springframework:spring-web:jar:5.0.7.RELEASE:compile
|  |  \- org.springframework:spring-beans:jar:5.0.7.RELEASE:compile
|  \- org.springframework:spring-webmvc:jar:5.0.7.RELEASE:compile
|     +- org.springframework:spring-aop:jar:5.0.7.RELEASE:compile
|     +- org.springframework:spring-context:jar:5.0.7.RELEASE:compile
|     \- org.springframework:spring-expression:jar:5.0.7.RELEASE:compile
+- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.0.3.RELEASE:compile
|  +- org.springframework.boot:spring-boot-starter-aop:jar:2.0.3.RELEASE:compile
|  |  \- org.aspectj:aspectjweaver:jar:1.8.13:compile
|  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.3.RELEASE:compile
|  |  +- com.zaxxer:HikariCP:jar:2.7.9:compile
|  |  \- org.springframework:spring-jdbc:jar:5.0.7.RELEASE:compile
|  +- org.hibernate:hibernate-core:jar:5.3.1.Final:compile
|  |  +- javax.persistence:javax.persistence-api:jar:2.2:compile
|  |  +- org.javassist:javassist:jar:3.22.0-GA:compile
|  |  +- net.bytebuddy:byte-buddy:jar:1.7.11:compile
|  |  +- antlr:antlr:jar:2.7.7:compile
|  |  +- org.jboss:jandex:jar:2.0.3.Final:compile
|  |  +- dom4j:dom4j:jar:1.6.1:compile
|  |  \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.3.Final:compile
|  +- javax.transaction:javax.transaction-api:jar:1.2:compile
|  +- org.springframework.data:spring-data-jpa:jar:2.0.8.RELEASE:compile
|  |  +- org.springframework.data:spring-data-commons:jar:2.0.8.RELEASE:compile
|  |  +- org.springframework:spring-tx:jar:5.0.7.RELEASE:compile
|  |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
|  \- org.springframework:spring-aspects:jar:5.0.7.RELEASE:compile
+- org.springframework.boot:spring-boot-starter-integration:jar:2.0.3.RELEASE:compile
|  \- org.springframework.integration:spring-integration-core:jar:5.0.6.RELEASE:compile
|     +- org.springframework:spring-messaging:jar:5.0.7.RELEASE:compile
|     +- org.springframework.retry:spring-retry:jar:1.2.2.RELEASE:compile
|     \- io.projectreactor:reactor-core:jar:3.1.8.RELEASE:compile
|        \- org.reactivestreams:reactive-streams:jar:1.0.2:compile
+- org.springframework.integration:spring-integration-http:jar:5.0.6.RELEASE:compile
+- org.springframework.integration:spring-integration-jpa:jar:5.0.6.RELEASE:compile
|  \- org.springframework:spring-orm:jar:5.0.7.RELEASE:compile
+- commons-fileupload:commons-fileupload:jar:1.3.3:compile
|  \- commons-io:commons-io:jar:2.2:compile
+- com.h2database:h2:jar:1.4.197:runtime
+- net.rakugakibox.spring.boot:orika-spring-boot-starter:jar:1.6.0:compile
|  \- ma.glasnost.orika:orika-core:jar:1.5.2:compile
|     +- com.thoughtworks.paranamer:paranamer:jar:2.8:compile
|     +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.4.2:compile
|     +- com.carrotsearch:java-sizeof:jar:0.0.4:compile
|     \- org.codehaus.janino:janino:jar:3.0.8:compile
|        \- org.codehaus.janino:commons-compiler:jar:3.0.8:compile
+- sk.cfh.demo:demo-rest-docs-api:jar:1.0-SNAPSHOT:compile
+- org.springframework.boot:spring-boot-starter-test:jar:2.0.3.RELEASE:test
|  +- org.springframework.boot:spring-boot-test:jar:2.0.3.RELEASE:test
|  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.3.RELEASE:test
|  +- com.jayway.jsonpath:json-path:jar:2.4.0:test
|  |  \- net.minidev:json-smart:jar:2.3:test
|  |     \- net.minidev:accessors-smart:jar:1.2:test
|  |        \- org.ow2.asm:asm:jar:5.0.4:test
|  +- junit:junit:jar:4.12:test
|  +- org.assertj:assertj-core:jar:3.9.1:test
|  +- org.mockito:mockito-core:jar:2.15.0:test
|  |  +- net.bytebuddy:byte-buddy-agent:jar:1.7.11:test
|  |  \- org.objenesis:objenesis:jar:2.6:test
|  +- org.hamcrest:hamcrest-core:jar:1.3:test
|  +- org.hamcrest:hamcrest-library:jar:1.3:test
|  +- org.skyscreamer:jsonassert:jar:1.5.0:test
|  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
|  +- org.springframework:spring-core:jar:5.0.7.RELEASE:compile
|  |  \- org.springframework:spring-jcl:jar:5.0.7.RELEASE:compile
|  +- org.springframework:spring-test:jar:5.0.7.RELEASE:test
|  \- org.xmlunit:xmlunit-core:jar:2.5.1:test
+- org.springframework.restdocs:spring-restdocs-mockmvc:jar:2.0.1.RELEASE:test
|  +- javax.servlet:javax.servlet-api:jar:3.1.0:test
|  \- org.springframework.restdocs:spring-restdocs-core:jar:2.0.1.RELEASE:test
\- org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
   +- org.apiguardian:apiguardian-api:jar:1.0.0:test
   +- org.junit.platform:junit-platform-engine:jar:1.2.0:test
   |  +- org.junit.platform:junit-platform-commons:jar:1.2.0:test
   |  \- org.opentest4j:opentest4j:jar:1.1.0:test
   \- org.junit.jupiter:junit-jupiter-api:jar:5.1.1:test

推荐答案

首先你需要注册一个 StandardServletMultipartResolver 作为 DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME bean: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-multipart

First of all you need to register a StandardServletMultipartResolver as a DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME bean: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-multipart

然后您需要将 MultiValueMap 作为下游的有效负载.只有在那里你才能对地图的每个部分做一些事情.

Then you need to expect a MultiValueMap<String, ?> as a payload downstream. And only there you can do something with each part of the map.

另请参阅有关此事的一些 Spring 集成示例:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#multipart-rest-inbound

Also see some Spring Integration sample on the matter: https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#multipart-rest-inbound

这篇关于Spring Integration dsl - HTTP 入站网关使用 multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆