代码之家  ›  专栏  ›  技术社区  ›  Bully

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

  •  0
  • Bully  · 技术社区  · 6 年前

    我想通过http inboud网关使用多部分/表单数据。第一部分是作为json的object,我想按内容类型将其序列化为object:application/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内部网关

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

    编辑:

    我在SpringBoot2.0.3.RELEASE上试过。看来它和 org.springframework.web.multipart.commons.CommonsMultipartResolver org.springframework.web.multipart.support.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
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Artem Bilan    6 年前

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

    那么你需要期待 MultiValueMap<String, ?>

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