没有为类型[java.io.ByteArrayOutputStream]找到合适的HttpMessageConverter并接受类型[[application / pdf]

时间:2016-09-01 19:24:59

标签: spring-integration

我使用Spring Integration调用服务,服务返回字节数组,但低于错误。

public ResponseEntity<byte[]> getInvoiceResponse(Long id, Long releaseId) throws Exception {

    ByteArrayOutputStream stream = (ByteArrayOutputStream) Client.getInvoice(id,sellerId);

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<byte[]>(stream.toByteArray(), headers, HttpStatus.CREATED);
}
<bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<int-http:inbound-gateway id="invoiceAPIEndoint" request-channel="httpRequestinvoiceAPIEndpointChannel"
                      supported-methods="GET"
                      path="/seller/invoice/{sellerId}/{orderReleaseId}/" message-converters="byteArrayMessageConverter" >


<int-http:header name="releaseId" expression="#pathVariables.orderReleaseId"/>
<int-http:header name="sellerId" expression="#pathVariables.id"/>
</int-http:inbound-gateway>
<int:service-activator input-channel="httpRequestinvoiceAPIEndpointChannel" expression="@sellerClientHelper.getSellerInvoice(headers.id,headers.releaseId)"/>

Apache Tomcat / 7.0.59 - 错误报告

HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.integration.MessagingException:无法转换回复:没有为类型[java.io.ByteArrayOutputStream]找到合适的HttpMessageConverter并接受类型[[application / pdf]]

类型异常报告

消息请求处理失败;嵌套异常是org.springframework.integration.MessagingException:无法转换回复:没有为类型[java.io.ByteArrayOutputStream]找到合适的HttpMessageConverter并接受类型[[application / pdf]]

描述服务器遇到内部错误,无法完成此请求。

异常

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.integration.MessagingException: Could not convert reply: no suitable HttpMessageConverter found for type [java.io.ByteArrayOutputStream] and accept types [[application/pdf]]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:200)
    net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:178)

< p> 根本原因 org.springframework.integration.MessagingException:无法转换回复:没有为类型[java.io.ByteArrayOutputStream]找到合适的HttpMessageConverter并接受类型[[application / pdf]]     org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.writeResponse(HttpRequestHandlingMessagingGateway.java:150)     org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:115)     org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)     javax.servlet.http.HttpServlet.service(HttpServlet.java:620)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)     javax.servlet.http.HttpServlet.service(HttpServlet.java:727)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:200)     net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:178)

1 个答案:

答案 0 :(得分:2)

对不起,问题是什么?

如果您想获得帮助,您应该描述问题并告诉我们您的期望。

至于我的消息:

  

找不到类型[java.io.ByteArrayOutputStream]的合适的HttpMessageConverter并接受类型[[application / pdf]]

有效,因为你有一个像:

这样的配置
 message-converters="byteArrayMessageConverter"

覆盖所有默认转换器,ByteArrayHttpMessageConverter具有此逻辑:

public boolean supports(Class<?> clazz) {
    return byte[].class == clazz;
}

因此,只有byte[]可以通过此转换器进行转换,其中ByteArrayOutputStream

这就解释了事情是如何发生的。

现在问问题,我们会尽力帮忙!