Search code examples
spring-cloud-streamspring-cloud-dataflow

String Cloud Stream httpclient processor expected-response-type as JSON


I am using httpclient processor as one of my steps, as follows (specified in Shell syntax as I'm testing locally, newlines added for readability here):

dataflow:>stream create --name test <sourceConfig> | 
  httpclient --httpclient.urlExpression='''https://host/restUrl''' 
    --httpclient.body-expression=null 
    --headers-expression=\"{Accept: 'application/json', <otherHeaders>}\" 
  | log" --deploy

The service returns a Map with a single entry, with a list as a value.

{ "key" : [ { "name" : "theName", ... }, ... ] }

By default, the REST call response body is interpreted as a String. I'd like to specify a --httpclient.expected-response-type option to have the body parsed into JSON. Per the docs, I need to specify a java.lang.Class<?> for this, and I'm not sure what class to specify, even after much digging in docs, unit tests, and POMs. Does anyone know?

Or, do I need to perform the conversion manually in the --reply-expression? Either way, I think I still need to know which JSON library Spring Cloud Stream is using, unless I'm missing something.


Solution

  • Well looks like you are missing something here in the knowledge. The JSON is a string repesentation of data in some specific format.

    That is already up to your service to convert that string to the target class. The same JSON different service may parse to different domain object. I can convert it into class Foo, you - to Bar. That is how that expected-response-type is provided to be protocol free and let you to decide to what to convert that JSON.

    The Jackson processor is used for converting to/from JSON. But again: the target POJO is your own class.

    You can convert JSON string to the java.util.Map though, but it isn't better than just string representation of JSON.

    More over it is a processor and it sends its result to the next step in the microservices chain, over the network. When you convert that JSON to POJO, it will converted to the JSON back on the binder's producer side. Does it make sense to convert it here for nothing?

    There is also JsonPropertyAccessor for SpEL which let you to get access to the field in the target JSON without converting it into the POJO.

    So, consider do not worry about JSON conversion and just receive it from the REST service and send it out as is - in string repesentation!