java 使用HttpRequestHandlingMessagingGateway在Spring集成中上传一个xml文件

mcvgt66p  于 2023-02-14  发布在  Java
关注(0)|答案(2)|浏览(192)

我正在尝试将一个多部分表单数据和一个附加的xml文件上传到integration server。
我使用的是带有RequestMapping bean的HttpRequestHandlingMessagingGateway

@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

但是当我试图上传xml文件时,消息是空白的。
如何在Message〈?〉中找到xml文件或如何检查Request对象?

t2a7ltrp

t2a7ltrp1#

下面是一个简单的测试,演示如何使用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());
        }

    }

}

注意:CommonsMultipartResolver已经过时一段时间了,并且已经从最新的Spring中删除。请确保您使用的是最新版本的框架:https://spring.io/projects/spring-integration#learn

zy1mlcev

zy1mlcev2#

我所做的就是创建一个接口

@Configuration
@MessagingGateway
@EnableIntegration
public interface IntegrationGateway2 {

 @Gateway(requestChannel = "Inbound_CHANNEL_in", replyChannel = "Inbound_Channel_reply")
 public Message<?> sendAndReceive(Message<?> input);
}

然后创建一个正常的REST控制器并获取该多部分文件,然后将其转换为消息。

@RestController
@EnableIntegration
@Configuration
public class SelerixController {
   
    @Bean("Inbound_CHANNEL_in")
    public MessageChannel Inbound_Channel_in() {
        return new DirectChannel();
    }
    @Bean("Inbound_Channel_reply")
    public MessageChannel Inbound_Channel_reply() {
        return new DirectChannel();
    }

    @Autowired
    IntegrationGateway integrationGateway;

    @PostMapping("/processFile")
    public ResponseEntity<String> fileUpload(@RequestParam String partner, @RequestParam("file") MultipartFile file ) throws IOException {
        Message reply = null;
        String xmlInputData = "";
        if (file != null) {
            InputStream is = file.getInputStream();
            xmlInputData = new BufferedReader(new InputStreamReader(is))
                    .lines().collect(Collectors.joining(""));
            MapConversionUtility mcu = new MapConversionUtility();
            String json = mcu.convertXmlToJSON(xmlInputData);

            Map<String, Object> header = new HashMap<String, Object>();
            header.put("uuid", UUID.randomUUID().toString());
            header.put("REAL_TIME_FLAG", "TRUE"); //QUERY_PARAM
            header.put("QUERY_PARAM", partner);
            Message<?> request = MessageBuilder
                    .withPayload(json)
                    .copyHeaders(header)
                    .build();
            reply = integrationGateway.sendAndReceive( request);
            LOGGER.info("getting reply final *************** {}",reply.getPayload());
        }
    }
}

相关问题