Kotlin数据类Jackson @JsonProperty不予尊重

时间:2017-11-07 01:54:41

标签: jackson kotlin feign

我使用Kotlin数据类将Feign连接到POST。

我打电话的API预计{..." brandInfo":{" TPID":1} ...}

我的理解是,如果没有正确选择jackson-module-kotlin依赖,那么Feign将无法进行POST,因为Jackson编码将完全失败。

然而,Feign能够发布,而杰克逊能够进行编码,但无论我做什么,发布的是{..." brandInfo":{&#34 ; tpid":1} ...},尽管brandInfo val使用@JsonProperty注释(" TPID")。

我错过了什么?

@JsonIgnoreProperties(ignoreUnknown = true)
data class KBrandInfo (
    @JsonProperty("TPID") //not honored
    val TPID: Long
)


interface KConversationServiceClient {
    @RequestLine("POST v2/conversations")
    @Headers("Content-Type: application/json")
    fun createConversation(createConversation: KCreateConversation): String
}


@Provides
public KConversationServiceClient getKConversationServiceClient(
        @Named("conversationServiceUrl") String baseUri,
        Feign.Builder builder) {
    return builder
            .logLevel(Logger.Level.FULL)
            .decoder(new StringDecoder())
            .encoder(new JacksonEncoder(jacksonObjectMapper())) //does this need some extra configuration in order to pick up @JsonProperty annotations?
            .requestInterceptor(requestTemplate ->
                    requestTemplate
                            .header("requestId", UUID.randomUUID().toString())
            .target(KConversationServiceClient.class, baseUri);
}


<properties>
    ...
    <feign.version>9.3.1</feign.version>
    <kotlin.version>1.1.51</kotlin.version>
    ...
</properties>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-core</artifactId>
        <version>${feign.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-hystrix</artifactId>
        <version>${feign.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-slf4j</artifactId>
        <version>${feign.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-jackson</artifactId>
        <version>${feign.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-gson</artifactId>
        <version>${feign.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jre8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test-junit</artifactId>
        <version>${kotlin.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-reflect</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-kotlin</artifactId>
        <version>2.7.9</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-parameter-names</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>${fasterxml.jackson.version}</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <!--Kotlin + Java + Maven implies some funky temporal dependencies and handling of annotation processing when building-->
        <!--* https://kotlinlang.org/docs/reference/using-maven.html-->
        <!--* https://kotlinlang.org/docs/reference/kapt.html-->
        <!--* https://github.com/JetBrains/kotlin-examples/blob/master/maven/dagger-maven-example/pom.xml-->
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <source>src/main/kotlin</source>
                            <source>src/main/java</source>
                            <source>target/generated-sources/annotations</source>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
        ...

1 个答案:

答案 0 :(得分:1)

对我有用的是指定注释应该应用于getter:@get:JsonProperty("TPID")