Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jackson with Kotlin:如何仅序列化带注释的属性_Java_Kotlin_Serialization_Jackson - Fatal编程技术网

Java Jackson with Kotlin:如何仅序列化带注释的属性

Java Jackson with Kotlin:如何仅序列化带注释的属性,java,kotlin,serialization,jackson,Java,Kotlin,Serialization,Jackson,我正试图在我现有的Java项目中开始使用Kotlin,但Jackson没有在我的Kotlin对象中检测到@JsonProperty(尽管它可以完美地与Java对象一起工作) 有没有办法配置Jackson,让它像对Java一样对Kotlin起作用 下面是Java类: public class TestJavaObj { @JsonProperty private String includeMe; private String dontIncludeMe; pub

我正试图在我现有的Java项目中开始使用Kotlin,但Jackson没有在我的Kotlin对象中检测到@JsonProperty(尽管它可以完美地与Java对象一起工作)

有没有办法配置Jackson,让它像对Java一样对Kotlin起作用

下面是Java类:

public class TestJavaObj {
    @JsonProperty
    private String includeMe;
    private String dontIncludeMe;

    public TestJavaObj(String includeMe, String dontIncludeMe) {
        this.includeMe = includeMe;
        this.dontIncludeMe = dontIncludeMe;
    }
}
还有Kotlin班:

class TestKotlinObj(
    @JsonProperty val includeMe: String,
    val dontIncludeMe : String?
)
以下是测试代码:

public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper()
            .registerModule(new KotlinModule()) //jackson-module-kotlin
            .configure(MapperFeature.AUTO_DETECT_GETTERS, false)
            .configure(MapperFeature.AUTO_DETECT_CREATORS, false)
            .configure(MapperFeature.AUTO_DETECT_FIELDS, false)
            .configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false)
            .configure(MapperFeature.AUTO_DETECT_SETTERS, false);

    TestJavaObj javaObj = new TestJavaObj("hello", "world");
    TestKotlinObj kotlinObj = new TestKotlinObj("hello", "world");

    System.out.println("Expected: " + mapper.writeValueAsString(javaObj));
    System.out.println("Got: " + mapper.writeValueAsString(kotlinObj));
}
以下是输出:

Expected: {"includeMe":"hello"}
Got: {}
我的gradle文件中的版本号:

kotlin_version = '1.3.72'
...
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" 
...
apply plugin: 'kotlin'
...
compile('com.fasterxml.jackson.core:jackson-annotations:2.10.3')
compile('com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8')
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

尝试将Kotlin类更改为:

class TestKotlinObj {
    @JsonProperty
    val includeMe: String;
    val dontIncludeMe : String;

    constructor(includeMe: String, dontIncludeMe: String) {
      this.includeMe = includeMe;
      this.dontIncludeMe = dontIncludeMe;
    }
}
您当前拥有的Kotlin类可能与java中的类似(这显然不起作用):

指定注释:

类TestKotlinObj(
@get:JsonProperty val includeMe:String,
val dontIncludeMe:字符串
)
结果:

Expected: {"includeMe":"hello"}
Got: {"includeMe":"hello"}

背景:

转换为Java字节码的类只有一个带注释的构造函数参数:

公共最终类TestKotlinObj{
@NotNull//此处没有注释
私有最终字符串includeMe;
@NotNull
私有最终字符串dontIncludeMe;
@NotNull//nothere
公共最终字符串getIncludeMe(){
返回此文件。includeMe;
}
@NotNull
公共最终字符串getDontIncludeMe(){
返回此。dontIncludeMe;
}
//但是这里
//VVV
公共TestKotlinObj(@JsonProperty@NotNull String includeMe,@NotNull String dontIncludeMe){
本质。检查参数不完整(includeMe,“includeMe”);
本质。检查参数完整(dontIncludeMe,“dontIncludeMe”);
超级();
this.includeMe=includeMe;
this.dontIncludeMe=dontIncludeMe;
}
}
序列化对象时不考虑这一点


请参阅相关问题:

尝试这个链接@unzila,它似乎是关于向bean类添加自定义序列化程序的。我要做的是让Kotlin类序列化与Java序列化的工作方式相同。谢谢,这非常有用。出于好奇,有没有一种简单的方法可以看出Kotlin类的Java等价物是什么?或者你只是知道它是这样工作的?没问题,当然。如果您使用Intellij IDEA,您可以:打开文件,进入工具->Kotlin->显示Kotlin字节码->反编译。另见问题
Expected: {"includeMe":"hello"}
Got: {"includeMe":"hello"}