启用缩小功能后,JsonProperty无法正常工作 [英] JsonProperty not working while minification enabled

查看:192
本文介绍了启用缩小功能后,JsonProperty无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程

class CodeRequest(@JsonProperty("phone") val phoneNumber: String)

当我以此类的对象作为主体发送请求(使用改型)时(未启用缩小功能),一切正常,请求将以{"phone": "123"}

When I send request (using retrofit) with an object of this class as body (while minification is not enabled) everything works and request will be send in this form {"phone": "123"}

但是使用以下proguard-rules.pro启用缩小功能将导致{"phoneNumber": "123"}请求正文.

But enabling minification with the following proguard-rules.pro will result in a {"phoneNumber": "123"} request body.

# Jackson
-keep class com.fasterxml.jackson.databind.ObjectMapper {
    public <methods>;
    protected <methods>;
}
-keep class com.fasterxml.jackson.databind.ObjectWriter {
    public ** writeValueAsString(**);
}
-keep @com.fasterxml.jackson.annotation.* class * { *; }
-keep @com.fasterxml.jackson.annotation.** class * { *; }
-keep class com.fasterxml.** { *; }
-keepattributes *Annotation*,EnclosingMethod,Signature,Exceptions,InnerClasses
-keep class * {
    @com.fasterxml.jackson.annotation.* *;
}
-keep class * { @com.fasterxml.jackson.annotation.JsonProperty *;}

# Application classes that will be serialized/deserialized over Jackson
-keepclassmembers class my.application.data.models.** { *; }
-keepclassmembers class my.application.domain.network.rest.** { *; }

这里缺少什么?

谢谢.

推荐答案

发布问题几分钟后找到解决方案. 问题与proguardjackson无关,原因是Kotlin删除了存储在kotlin.Metadata中的所需数据. 添加以下规则以保护此问题:

Found solution a couple of minutes after posting the question. The problem is not with proguard nor jackson, it's that Kotlin erases required data which are stored in kotlin.Metadata. Adding the following rule to proguard fixed the issue:

-keep class kotlin.Metadata { *; }

这篇关于启用缩小功能后,JsonProperty无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆