1
votes

I am calling service using Spring Integration and service returns byte array but getting below error.

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 - Error report

HTTP Status 500 - 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]]

type Exception report

message 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]]

description The server encountered an internal error that prevented it from fulfilling this request.

exception

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)

root cause org.springframework.integration.MessagingException: Could not convert reply: no suitable HttpMessageConverter found for type [java.io.ByteArrayOutputStream] and accept types [[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

1 Answers

2
votes

Sorry, what is the question?

If you would like to get a help you should describe the problem and let us know what you expect.

As for me the message:

no suitable HttpMessageConverter found for type [java.io.ByteArrayOutputStream] and accept types [[application/pdf]]

Is valid, because you have a config like:

 message-converters="byteArrayMessageConverter"

which overrides all default converters and where ByteArrayHttpMessageConverter has this logic:

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

So, only byte[] can be converted by this converter, where your is ByteArrayOutputStream.

That is explanation how things happen.

Now ask the question and we will try to help!