javaxmlspring-integrationspring-integration-http

Upload a xml file in spring integration using HttpRequestHandlingMessagingGateway


I am trying to upload a multipart form-data with an attached xml file to integration server.

I am using a HttpRequestHandlingMessagingGateway with a RequestMapping bean.

  @Bean("Inbound_GATEWAY_in")
    public MessageChannel Inbound_GATEWAY_in() { return new DirectChannel(); }
  @Bean
    public HttpRequestHandlingMessagingGateway selerixInboundRequest() {
        HttpRequestHandlingMessagingGateway gateway =
                new HttpRequestHandlingMessagingGateway(true);
        gateway.setRequestMapping(selerixMapping());
        gateway.setMessageConverters( messageConverter() );
        gateway.setMultipartResolver(multipartResolverBean());
        gateway.setRequestTimeout(3000); // 3s
        gateway.setReplyTimeout(5000); // 5s
        gateway.setRequestChannelName("Inbound_GATEWAY_in");
        gateway.setReplyChannelName("Outbound_GATEWAY_out");
        return gateway;
    }
    @Bean
    public RequestMapping selerixMapping() {
        RequestMapping requestMapping = new RequestMapping();
        requestMapping.setPathPatterns("/path");
        requestMapping.setMethods(HttpMethod.POST);
        requestMapping.setConsumes(MediaType.MULTIPART_FORM_DATA_VALUE);
        return requestMapping;
    }
@Bean
    public MultipartResolver multipartResolverBean(){
        return new CommonsMultipartResolver();
    }
@ServiceActivator(inputChannel = "Inbound_GATEWAY_in")
    public Message<?>  headerEnrich_Inbound_GATEWAY_in(Message<?> message){
         Message<?> outmessage = null;
    LOGGER.info("message ", message); // returns blank message

But when I am trying to upload the xml file the message is coming as blank.

How can I find the xml file in the Message<?> or how can I check the Request object ?


Solution

  • Here is a simple test to demonstrate how we can upload the file using Spring Integration:

    @SpringJUnitWebConfig
    @DirtiesContext
    public class FileUploadTests {
    
        @Autowired
        private WebApplicationContext wac;
    
    
        private MockMvc mockMvc;
    
        @BeforeEach
        public void setup() {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        }
    
        @Test
        void commonsFileUploadValidation() throws Exception {
            MockPart mockPart1 = new MockPart("file", "file.text", "ABC".getBytes(StandardCharsets.UTF_8));
            mockPart1.getHeaders().setContentType(MediaType.TEXT_PLAIN);
    
            this.mockMvc.perform(multipart("/path").part(mockPart1))
                    .andExpect(status().isOk())
                    .andExpect(content().string("File uploaded: file.text with content: ABC"));
        }
    
        @Configuration
        @EnableIntegration
        public static class ContextConfiguration {
    
            @Bean("Inbound_GATEWAY_in")
            public MessageChannel Inbound_GATEWAY_in() {
                return new DirectChannel();
            }
    
            @Bean
            public HttpRequestHandlingMessagingGateway selerixInboundRequest() {
                HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway();
                RequestMapping requestMapping = new RequestMapping();
                requestMapping.setPathPatterns("/path");
                gateway.setRequestMapping(requestMapping);
                gateway.setRequestChannelName("Inbound_GATEWAY_in");
                return gateway;
            }
    
            @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
            public MultipartResolver multipartResolver() {
                return new StandardServletMultipartResolver();
            }
    
            @ServiceActivator(inputChannel = "Inbound_GATEWAY_in")
            public String headerEnrich_Inbound_GATEWAY_in(MultiValueMap<String, MultipartFile> payload) throws IOException {
                MultipartFile file = payload.getFirst("file");
                return "File uploaded: " + file.getOriginalFilename() + " with content: " + new String(file.getBytes());
            }
    
        }
    
    }
    

    Note: the CommonsMultipartResolver is deprecated for a while and was removed from latest Spring. Please, be sure that you use the latest versions of the frameworks: https://spring.io/projects/spring-integration#learn