当前位置: 首页 > 知识库问答 >
问题:

Spring集成dsl-HTTP入站网关使用多部分/表单数据

梁丘招
2023-03-14

我想通过超文本传输协议使用multipart/form-data inboud网关。第一部分是json对象,我想按内容类型将其序列化为对象:应用程序/json。第二部分是pdf文件。是否可以简单地序列化?我需要我的自定义消息转换器吗?

原始请求:

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 Inboud网关

@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启动2.0.3上尝试过了。释放。它似乎不适用于org.springframework.web.multipart.commons.Common sMultipartResolver。在有效负载中,我总是看到空的LinkedMultiValueMap。但我注意到它在默认配置org.springframework.web.multipart.support.Standard ardServletMultipartResolver下正常工作。Common sMultipartResolver和Standard ardServletMultipartResolver有什么区别?

依赖项

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

共有1个答案

吴城
2023-03-14

首先,您需要将Standard ServletMultipartResolver注册为DispatcherServlet。MULTIPART_RESOLVER_BEAN_NAMEbean:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-multipart

然后你需要期待一个MultiValueMap

另请参阅有关该问题的一些Spring集成示例:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#multipart-rest入站

 类似资料:
  • 我尝试使用以下代码,得到了回应:状态:405方法不允许。这是我的Http请求:http://localhost:8090/services/test?name=test.代码或http请求有什么问题?

  • 这是一个Spring集成(基于Spring Boot)项目,我已经配置了Spring集成应用程序,如下所示, 配置xml: CustomConverter定义如下 对于多部分/表单数据,我在CustomConverter类中总是得到一个空白负载。 我还尝试添加一个多部分解析器,如下所示 我在stackoverflow Spring Integrdsl上关注了一个类似的问题——HTTP入站网关使用m

  • 我正在尝试将spring集成配置为向队列发送消息,然后接收消息,即非常简单的事情: 我认为解耦所必需的是在流程的两端都有一个消息网关。因此,我的第一次尝试(有效)如下所示: 其中MessageReceiverHandler()是扩展AbstractMessageHandler的bean。 所以上面我们有一个用于出站消息的消息网关。我假设我们也应该有一个用于入站消息的网关,允许我们将传入消息处理与应

  • 我不熟悉Spring集成。我正在尝试使用http入站网关构建一个简单的应用程序。下面是我得到的运行时异常。 下面是代码文件。 波约 服务 } 服务激活器 } 存储库 请帮助我,我正在试图找到异常发生的原因,但无法解决。提前谢谢。 集成文件。

  • 我试图使用Spring Integration和HTTP出站网关组合一个非常简单的HTTP POST示例。 我需要能够发送带有一些POST参数的HTTP POST消息,就像使用: 如果向接口方法发送一个简单的作为参数,我可以让它工作(没有POST参数),但我需要发送一个pojo,其中pojo的每个属性都成为POST参数。 我有以下SI配置: 我的接口如下所示: 我的类如下所示: 我为此搜索了大量的

  • 我需要关于spring集成jdbc出站网关的帮助。我目前正在使用spring集成示例jdbc。我添加代码来使用jdbc出站网关更新数据,下面是spring集成上下文。xml: 这是我的个人服务。爪哇: Main.java: 为什么我总是得到这个错误消息,每当我试图运行它? 那个人。java POJO: 请帮帮我,我不知道是哪种java。对象,因为我在代码中的任何地方都不使用它。谢谢