JBoss Netty with JSON

2019-07-03 18:07发布

I would like my Ajax code to connect a server through Netty. For that purpose I need a JSON decoder and encoder in the server side Netty handler.

Is there any out of the box implementation for this, or should I write my own?

Thanks,

Gil

标签: json netty
1条回答
女痞
2楼-- · 2019-07-03 18:23

As I know, there is no built in JSON decoder/encoder, but it does not mean that you have to start from basic HTTP Handlers.

1) Have the HttpRequestDecoder, HttpResponseEncoder in the server pipeline.

2) then implement HttpContentDecoder, HttpContentEncoder abstract classes for JSON decoding & encoding, here you have to implement the newContentDecoder, newContentEncoder methods by providing a OneToOneEncoder/Decoder implementation for JSON.

You can use Google Gson to write the OneToOneEncoder/Decoder implementation.

then add HttpContentDecoder, HttpContentEncoder implementations in the pipeline.

for more detail, you can have a look on HttpContentDecompressor, HttpContentCompressor source code.

查看更多
登录 后发表回答