You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/11/28 09:53:51 UTC

[GitHub] [dubbo] YangSihong opened a new issue, #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

YangSihong opened a new issue, #11042:
URL: https://github.com/apache/dubbo/issues/11042

   I tried to do this,But it didn't work at all。
    @Bean
       public HttpMessageConverters fastJsonHttpMessageConverters() {
           FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
           FastJsonConfig fastJsonConfig = new FastJsonConfig();
           fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
                   SerializerFeature.WriteMapNullValue);
           //处理日期时间格式化问题
           fastJsonConfig.setDateFormat("yyyy-MM-dd hh:mm:ss");
           //处理中文乱码问题
           List<MediaType> fastMediaTypes = new ArrayList<>();
           fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
           fastConvert.setSupportedMediaTypes(fastMediaTypes);
           fastConvert.setFastJsonConfig(fastJsonConfig);
           return new HttpMessageConverters((HttpMessageConverter<?>) fastConvert);
       }
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] AlbumenJ commented on issue #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

Posted by GitBox <gi...@apache.org>.
AlbumenJ commented on issue #11042:
URL: https://github.com/apache/dubbo/issues/11042#issuecomment-1330503172

   Dubbo should provide a way to support custom serilization in Rest protocol.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] YangSihong commented on issue #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

Posted by GitBox <gi...@apache.org>.
YangSihong commented on issue #11042:
URL: https://github.com/apache/dubbo/issues/11042#issuecomment-1354380132

   > 我看了一下,这个应该可以使用参数 extension 注入自定义的Provider来实现。 如:extension =com.mydubbo.config.GsonJsonProvider ,需实现MessageBodyWriter 和 MessageBodyReader 接口
   > 
   > ** @Provider @consumes({"application/json", "text/json"}) @produces({"application/json", "text/json"}) public class GsonJsonProvider implements MessageBodyReader, MessageBodyWriter **
   我一会试下这种方式,但感觉这种方式怪怪的。
   rest协议下不能像dubbo协议那样在protocol下指定序列化方式吗?然后自定义具体的序列化实现扩展方法。
   我是通过调整依赖包的方式把fastjson序列化给去掉了,目前默认的是resteasy,能够解决我当前空参数据过滤的问题,但我依然没解决如何自定义序列化扩展方法,比如:SerializerFeature.WriteMapNullValue等等


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] AlbumenJ commented on issue #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

Posted by GitBox <gi...@apache.org>.
AlbumenJ commented on issue #11042:
URL: https://github.com/apache/dubbo/issues/11042#issuecomment-1330503517

   We can enhance it in the next version.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] cnjxzhao commented on issue #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

Posted by GitBox <gi...@apache.org>.
cnjxzhao commented on issue #11042:
URL: https://github.com/apache/dubbo/issues/11042#issuecomment-1354436391

   > > 我看了一下,这个应该可以使用参数 extension 注入自定义的Provider来实现。 如:extension =com.mydubbo.config.GsonJsonProvider ,需实现MessageBodyWriter 和 MessageBodyReader 接口
   > > ** @Provider @consumes({"application/json", "text/json"}) @produces({"application/json", "text/json"}) public class GsonJsonProvider implements MessageBodyReader, MessageBodyWriter **
   > > 我一会试下这种方式,但感觉这种方式怪怪的。
   > > rest协议下不能像dubbo协议那样在protocol下指定序列化方式吗?然后自定义具体的序列化实现扩展方法。
   > > 我是通过调整依赖包的方式把fastjson序列化给去掉了,目前默认的是resteasy,能够解决我当前空参数据过滤的问题,但我依然没解决如何自定义序列化扩展方法,比如:SerializerFeature.WriteMapNullValue等等
   
   这个感觉上有那么一点点吧。不过通过定义新参数,像protocol内 增加serialization参数 也是可以的,实现与现有extension 类似。
   针对于你说的这个情况,可以在Provider内实现。
   如:
   1. FastJson 可以重新设置 FastJsonConfig。或者干脆一点继承 com.alibaba.fastjson.support.jaxrs.FastJsonProvider ,在构造函数内设置FastJsonConfig
   伪代码:
   public XXJsonProvider() {
   	super();
   	FastJsonConfig fastJsonConfig = new FastJsonConfig();
   	fastJsonConfig.setSerializerFeatures(
   			SerializerFeature.DisableCircularReferenceDetect,
   			SerializerFeature.WriteMapNullValue
   			....
   			);
   	this.setFastJsonConfig(fastJsonConfig);
   }
   
   2. Gson的话可以增加调用 serializeNulls()方法。
     gson = new GsonBuilder().serializeNulls().create(); 
   
   你先试试吧。


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] cnjxzhao commented on issue #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

Posted by GitBox <gi...@apache.org>.
cnjxzhao commented on issue #11042:
URL: https://github.com/apache/dubbo/issues/11042#issuecomment-1346470806

   Assign it to me. I'll try to fix it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] cnjxzhao commented on issue #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

Posted by GitBox <gi...@apache.org>.
cnjxzhao commented on issue #11042:
URL: https://github.com/apache/dubbo/issues/11042#issuecomment-1354308107

   我看了一下,这个应该可以使用参数 extension 注入自定义的Provider来实现。
   如:extension =com.mydubbo.config.GsonJsonProvider ,需实现MessageBodyWriter 和 MessageBodyReader 接口
   
   **
   @Provider
   @Consumes({"application/json", "text/json"})
   @Produces({"application/json", "text/json"})
   public class GsonJsonProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object>
   **


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] YangSihong commented on issue #11042: dubbo3.1 rest protocol :How to specify the SerializerFeature of fastjson

Posted by GitBox <gi...@apache.org>.
YangSihong commented on issue #11042:
URL: https://github.com/apache/dubbo/issues/11042#issuecomment-1354516518

   > > > 我看了一下,这个应该可以使用参数 extension 注入自定义的Provider来实现。 如:extension =com.mydubbo.config.GsonJsonProvider ,需实现MessageBodyWriter 和 MessageBodyReader 接口
   > > > ** @Provider @consumes({"application/json", "text/json"}) @produces({"application/json", "text/json"}) public class GsonJsonProvider implements MessageBodyReader, MessageBodyWriter **
   > > > 我一会试下这种方式,但感觉这种方式怪怪的。
   > > > rest协议下不能像dubbo协议那样在protocol下指定序列化方式吗?然后自定义具体的序列化实现扩展方法。
   > > > 我是通过调整依赖包的方式把fastjson序列化给去掉了,目前默认的是resteasy,能够解决我当前空参数据过滤的问题,但我依然没解决如何自定义序列化扩展方法,比如:SerializerFeature.WriteMapNullValue等等
   > 
   > 这个感觉上有那么一点点吧。不过通过定义新参数,像protocol内 增加serialization参数 也是可以的,实现与现有extension 类似。 针对于你说的这个情况,可以在Provider内实现。 如:
   > 
   > 1. FastJson 可以重新设置 FastJsonConfig。或者干脆一点继承 com.alibaba.fastjson.support.jaxrs.FastJsonProvider ,在构造函数内设置FastJsonConfig
   >    伪代码:
   >    public XXJsonProvider() {
   >    super();
   >    FastJsonConfig fastJsonConfig = new FastJsonConfig();
   >    fastJsonConfig.setSerializerFeatures(
   >    SerializerFeature.DisableCircularReferenceDetect,
   >    SerializerFeature.WriteMapNullValue
   >    ....
   >    );
   >    this.setFastJsonConfig(fastJsonConfig);
   >    }
   > 2. Gson的话可以增加调用 serializeNulls()方法。
   >    gson = new GsonBuilder().serializeNulls().create();
   > 
   > 你先试试吧。
   
   我比较不解的是,dubbo:protocol name="dubbo" 时,可以通过serialization方式切换序列化实现,但是dubbo:protocol name="rest"时,却无法通过serialization切换,如果rest协议下不支持serialization方式,是不是可以给出明确的运行时提示。
   
   目前实测extension的方式实现MessageBodyReader, MessageBodyWriter是可行的。感谢
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org