Announcement Announcement Module
Collapse
No announcement yet.
SerializingHttpMessageConverter with http outbound gateway Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SerializingHttpMessageConverter with http outbound gateway

    Hi,

    I have some data which came from a HTTP Get.

    The content type is application/octet-stream.

    The data is not correctly converted:

    Code:
            <int-http:outbound-gateway http-method="GET"
                                       expected-response-type="java.lang.String"
                                       url="${CACHE_SERVICE_PATH}{cacheKey}"
                                       request-factory="httpClientFactory"
                                       error-handler="cacheRequestErrorHandler">
                <int-http:uri-variable name="cacheKey" expression="payload"/>
            </int-http:outbound-gateway>
    Code:
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 springframework.web.client.RestTemplate : Created GET request for "HTTP://vlint-int.baba.com:7040/cache/v1/ac.baba.100000071001"
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 springframework.web.client.RestTemplate : Setting request Accept header to [text/plain, */*]
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 springframework.web.client.RestTemplate : GET request for "HTTP://vlint-int.baba.com:7040/cache/v1/ac.baba.100000071001" resulted in 200 (OK)
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 springframework.web.client.RestTemplate : Reading [java.lang.String] as "application/octet-stream" using [[email protected]54ca3]
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 integration.http.support.DefaultHttpHeaderMapper : inboundHeaderNames=[Accept-Ranges, Age, Allow, Cache-Control, Content-Encoding, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Type, Date, ETag, Expires, Last-Modified, Location, Pragma, Proxy-Authenticate, Refresh, Retry-After, Server, Set-Cookie, Trailer, Transfer-Encoding, Vary, Via, Warning, WWW-Authenticate]
    :
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 integration.http.support.DefaultHttpHeaderMapper : headerName=[Content-Type] WILL be mapped, matched pattern=Content-Type
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 integration.http.support.DefaultHttpHeaderMapper : setting headerName=[Content-Type], value=application/octet-stream
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 integration.http.support.DefaultHttpHeaderMapper : headerName=[Content-Length] WILL be mapped, matched pattern=Content-Length
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 integration.http.support.DefaultHttpHeaderMapper : setting headerName=[Content-Length], value=18
    :
    2012 Sep 11 17:23:56 http-bio-7050-exec-3 integration.http.outbound.HttpRequestExecutingMessageHandler : handler 'org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#44d40c4e' sending reply Message: [Payload=’t
                                                                       1408xxxxxxx][Headers={timestamp=1347409436426, id=b5a60612-0a2a-4ea0-9645-82d280ec5baf, errorChannel=org.springframework.integration.core.MessagingTemplate$TemporaryReplyChannel@19d449fc, Authorization=baba, replyChannel=org.springframework.integration.core.MessagingTemplate$TemporaryReplyChannel@19d449fc, Date=1347409436000, Content-Length=18, http_statusCode=200, Content-Type=application/octet-stream, Server=Apache-Coyote/1.1}]

    The payload has extra bytes because it was stored with writeObject(). See the red highlight.

    Without the message-converters set, StringHttpMessageConverter will just convert verbatim, hence with those extra bytes, which I don't want.

    The question:

    1. The expected data is String. Is using SerializingHttpMessageConverter the right way to do?
    2. How to inject SerializingHttpMessageConverter? With the correct MediaType? Specifically, I need to call setSupportedMediaTypes().

    Thanks so much!
    Simon
    Last edited by simonso; Sep 11th, 2012, 08:57 PM.

  • #2
    If you are using java serialization, the server should set the content type to 'application/x-java-serialized-object' and you can use a SerializingHttpMessageConverter (use the message-converters attribute to inject a SerializingHttpMessageConverter).

    If you can't control the server, just have the gateway return a byte array and use a PayloadDeserializingTransformer on the reply-channel to transform the byte[] to an Object.

    Or, create a customized MessageConverter (perhaps based on the SerializingHttpMessageConverter) that will perform the conversion with a non-standard MediaType.

    BTW, as a general rule, it's not a good idea to use java serialization between applications, consider using JSON or XML to reduce the coupling.

    Comment


    • #3
      Thanks Gary.

      Like any other integration projects, the server is not something I can control... Or I chose not to control.

      Based on your advice #3, I actually implemented my own serializingHttpMessageConverter with media type application/octet-stream.

      Originally I thought I could reuse the existing SerializingHttpMessageConverter, but I have difficulty setting the supported types... Not even sure if that's possible.

      I totally agree with you about the java serialization comment you made. Will work with my other team members to revisit this. In lieu of a binary protocol, I think JSON is as efficient and loosely coupled as it comes.

      Thanks again,
      Simon

      Comment


      • #4
        Yes, you can change the supported media types; see

        Code:
        	/**
        	 * Set the list of {@link MediaType} objects supported by this converter.
        	 */
        	public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) {
        		Assert.notEmpty(supportedMediaTypes, "'supportedMediaTypes' must not be empty");
        		this.supportedMediaTypes = new ArrayList<MediaType>(supportedMediaTypes);
        	}
        on the AbstractHttpMessageConverter; sorry I didn't point that out yesterday.

        Comment

        Working...
        X