多部分请求通过Junit工作正常,但是IE浏览器失败了

时间:2016-12-09 10:51:19

标签: spring-integration

我在项目中使用Spring Integration。我试图打一个多部分的网络服务。我正在使用int-http:outbound-gateway来执行web服务。我可以通过Junit执行休息服务而没有任何问题,但奇怪的是,当我尝试使用IE浏览器执行时,相同的服务失败。 以下是我得到的错误:

Caused by: org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [text/xml;charset=UTF-8]
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:665)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:409)
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:372)

可能是什么原因?如果您需要更多信息,请与我们联系。

<int:chain input-channel="Test-Rest-Channel"  >

        <int:header-filter  header-names="accept-encoding"/>    

         <int:service-activator  ref="httpOutboundGatewayHandler" method="buildMultipartHttpOutboundGatewayRequest" /> 

        <int-http:outbound-gateway  url-expression="headers.restResourceUrl"
                                    http-method-expression="headers.httpMethod"
                                    extract-request-payload="true"
                                    expected-response-type="java.lang.String">
        </int-http:outbound-gateway>

        <int:transformer expression="'&lt;response>' + payload + '&lt;/response>'"/>
        <int:service-activator ref="msgHandler" method="enrichPayloadXml" />

    </int:chain>

当我将org.springframework.core.io.FileSystemResource cannot be cast to java.lang.String Caused by: java.lang.ClassCastException: org.springframework.core.io.FileSystemResource cannot be cast to java.lang.String更改为Content-Type时,我收到错误application/x-www-form-urlencoded

完整的堆栈跟踪如下:

2016-12-13 12:50:56,311 DEBUG http-0.0.0.0-8080-5 [org.springframework.integration.http.support.DefaultHttpHeaderMapper] setting headerName=[Content-Type], value=application/x-www-form-urlencoded
2016-12-13 12:50:56,311 DEBUG http-0.0.0.0-8080-5 [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[originalPayload] WILL NOT be mapped
2016-12-13 12:50:56,311 DEBUG http-0.0.0.0-8080-5 [org.springframework.web.client.RestTemplate] Created POST request for "http://10.193.244.136/awdServer/awd/services/v1/instances/"
2016-12-13 12:50:56,311 DEBUG http-0.0.0.0-8080-5 [org.springframework.web.client.RestTemplate] Setting request Accept header to [text/plain, application/json, application/*+json, */*]
2016-12-13 12:50:56,311 DEBUG http-0.0.0.0-8080-5 [org.springframework.web.client.RestTemplate] Writing [{source=[file [C:\Users\dt212693\AppData\Local\Temp\temp2340685368206282153443984647956344\source.xml]], attachment=[file [C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg]]}] as "application/x-www-form-urlencoded" using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@660c43f2]
2016-12-13 12:50:56,311 WARN  http-0.0.0.0-8080-5 [org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway] failure occurred in gateway sendAndReceive
org.springframework.integration.MessageHandlingException: HTTP request execution failed for URI [http://10.193.244.136/awdServer/awd/services/v1/instances/]
                at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:398)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:142)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:148)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:228)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:212)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:177)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:171)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:149)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:148)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:228)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:212)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:177)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:171)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:149)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:131)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:115)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:102)
                at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:178)
                at org.springframework.integration.handler.MessageHandlerChain$ReplyForwardingMessageChannel.send(MessageHandlerChain.java:256)
                at org.springframework.integration.handler.MessageHandlerChain$ReplyForwardingMessageChannel.send(MessageHandlerChain.java:249)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:228)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:212)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:177)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:171)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:149)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:148)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:228)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:212)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:177)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:167)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:149)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:131)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:115)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:102)
                at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:178)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:149)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.router.AbstractMessageRouter.handleMessageInternal(AbstractMessageRouter.java:134)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:148)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:228)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:212)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:177)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:171)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:149)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:148)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:228)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:212)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:177)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:171)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:149)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:131)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:115)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:102)
                at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:178)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:149)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.router.AbstractMessageRouter.handleMessageInternal(AbstractMessageRouter.java:150)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:115)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:102)
                at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:178)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:149)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:169)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:228)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:212)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:177)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:171)
                at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:149)
                at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:115)
                at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:102)
                at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
                at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:178)
                at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:330)
                at org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:361)
                at org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:274)
                at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:234)
                at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:208)
                at org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.actualDoHandleRequest(HttpRequestHandlingEndpointSupport.java:478)
                at org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:380)
                at org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:101)
                at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
                at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
                at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
                at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
                at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
                at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
                at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
                at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
                at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: org.springframework.core.io.FileSystemResource cannot be cast to java.lang.String
                at org.springframework.http.converter.FormHttpMessageConverter.writeForm(FormHttpMessageConverter.java:232)
                at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:196)
                at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:72)
                at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:656)
                at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
                at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
                at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:409)
                at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:372)
                ... 138 more

当我将Content-Type标题用作multipart/form-data时,我收到以下错误:

Caused by: org.springframework.web.client.HttpClientErrorException: 403 Forbidden
                at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
                at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:532)
                at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:488)
                at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
                at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:409)
                at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:372)
                ... 138 more

编辑标题Content-Type multipart/form-data与JUNIT合作正常。

当我将Accept标题用作multipart/form-data时,我收到以下错误:

Caused by: org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [text/xml;charset=UTF-8]
                at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:665)
                at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
                at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
                at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:409)
                at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:372)
                ... 138 more

1 个答案:

答案 0 :(得分:0)

  

找不到合适的HttpMessageConverter请求类型[java.util.HashMap]和内容类型[text / xml; charset = UTF-8]

我认为您的JUnit测试使用不同的有效负载类型进行操作。但是这里有Map有效载荷,实际上无法转换为text/xml

或提供一些转换器以将地图转换为适合的XML转换类型,或更改content-type标头以将该地图序列化为application/x-www-form-urlencoded

<强>更新

Caused by: java.lang.ClassCastException: org.springframework.core.io.FileSystemResource cannot be cast to java.lang.String
            at org.springframework.http.converter.FormHttpMessageConverter.writeForm(FormHttpMessageConverter.java:232)
            at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:196)

Map似乎包含一些FileSystemResource但不是String的值。从另一方面来说,你没有通过isMultipart条件而我猜是因为你错了contentType

private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType) {
    if (contentType != null) {
        return MediaType.MULTIPART_FORM_DATA.includes(contentType);
    }

不确定multipart/form-data是否为contentType,但您确实应该确定您希望发送多部分,而不是普通表单。

真的要使用:

 msgBuild.setHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);