Spring XD: “tcp” source outputs byte array instead of string? How to output regular text?

大憨熊 提交于 2019-12-24 15:25:37

问题


The goal is to read data emitted over the network.

On the data generation side I have an app which spews to stdout. The content of this data is a JSON string.

Here's what I'm doing (on Linux Mint 17, using an BSD flavored netcat):

data generation:

my_app_which_outputs_json | netcat localhost 9999

In SpringXD: (with xd-singlenode)

xd:>stream create --name tcptest --definition "tcp --decoder=LF --port=9999 | file " --deploy
Created and deployed new stream 'tcptest'

Output:

/tmp/xd/output$ cat tcptest.out 
82,117,110, ...  (etc, lots more bytes)

I'm sure this is user error, but not sure what to change to make it right.

I should note that if I do this, it works as expected:

my_app_which_outputs_json > /tmp/somefile.txt
...
xd:>stream create --name filetest --definition "tail --name=/tmp/somefile.txt | file" --deploy

回答1:


Following up to your own answer, the converters are currently not configured to understand that a byte[] can be converted to a String with content type application/json.

Of course, even if we configured it to do so, it wouldn't be able to determine if the content really is JSON.




回答2:


I added the following to my stream defn and it is now doing what I expected. I found this in the "Type Conversion" section of the documentation.

stream create --name tcptest \
    --definition "tcp --decoder=LF --port=9999 --outputType=text/plain \
    | file " --deploy

(The newlines and backwhacks are not in my actual code, but are used for readability.)

I also tried ... --outputType=application/json... which did not work. Not entirely sure why.




回答3:


text/plain with a byte[] payload activates a ByteArrayToStringMessageConverter. application/json does not currently. I have created https://jira.spring.io/browse/XD-2512 to address this



来源:https://stackoverflow.com/questions/27491237/spring-xd-tcp-source-outputs-byte-array-instead-of-string-how-to-output-regu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!