问题描述
我希望我的Ajax代码通过Netty连接服务器.为此,我需要在服务器端Netty处理程序中使用JSON解码器和编码器.
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?
谢谢
吉尔
推荐答案
据我所知,没有内置的JSON解码器/编码器,但这并不意味着您必须从基本的HTTP处理程序开始.
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)在服务器管道中具有HttpRequestDecoder,HttpResponseEncoder.
1) Have the HttpRequestDecoder, HttpResponseEncoder in the server pipeline.
2)然后实现HttpContentDecoder,HttpContentEncoder抽象类,以进行JSON解码&编码,在这里您必须通过提供JSON的OneToOneEncoder/Decoder实现来实现newContentDecoder,newContentEncoder方法.
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.
您可以使用Google Gson编写OneToOneEncoder/Decoder实现.
You can use Google Gson to write the OneToOneEncoder/Decoder implementation.
然后在管道中添加HttpContentDecoder和HttpContentEncoder实现.
then add HttpContentDecoder, HttpContentEncoder implementations in the pipeline.
有关更多详细信息,您可以查看HttpContentDecompressor,HttpContentCompressor源代码.
for more detail, you can have a look on HttpContentDecompressor, HttpContentCompressor source code.
这篇关于带有JSON的JBoss Netty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!