添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
气势凌人的稀饭  ·  2021 | ...·  2 年前    · 
千年单身的猴子  ·  gradle - Failed to ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Disclaimer

I know there are many posts with a similar problem, but no solution is working for me. I've been trying since yesterday and the amount of attempts I tried are too many to be listed here. What I'm posting is the latest attempt.

Overview

I have a service, that sends a request to another service and needs to deserialize the response polymorphically.

Classes

This is the format I receive from the rest call. Only two properties.

data class ExecutionPayload(
    val type: ChangeRequestType,
    val data: ExecutionData

ExecutionData is the polymorphic bit.

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
    Type(value = UpdateNetworkConfigurationsExecutionData::class, name = "UpdateNetworkConfigurationsExecutionData")
sealed class ExecutionData
@JsonIgnoreProperties(ignoreUnknown = true)
data class UpdateNetworkConfigurationsExecutionData(
    @JsonProperty("type") val updatedProfiles: List<UpdateSalesChannelProfileViewModel>,
    @JsonProperty("type") val zoning: List<SoftZoningConfig>
) : ExecutionData()

Details

I'll add some more context, which might be obvious to you, but nevertheless.

ExecutionData will in the future have sublcasses with a completely different data structure than UpdateNetworkConfigurationsExecutionData.

Silly example

data class SomeOtherConfigurationUpdateExecutionData(
    val updateSomethingHere: String,
    val anotherPropertyUpdate: Int
    val anExtraProperty: List<SomeConfig>
) : ExecutionData()

Problem

I keep getting this error no matter how hard I try.

ERROR com.somepackage.utils.Logger - CustomErrorConfig - org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Could not resolve subtype of [simple type, class com.somepackage.ExecutionData]: missing type id property 'type' (for POJO property 'data');
nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.somepackage.ExecutionData]:
missing type id property 'type' (for POJO property 'data')

I would really appreciate your help.

P.S. I'm new to kotlin, and jackson.

The first thing you need to do is remove the unnecessary @JsonProperty("type"):

@JsonIgnoreProperties(ignoreUnknown = true)
data class UpdateNetworkConfigurationsExecutionData(
    val updatedProfiles: List<UpdateSalesChannelProfileViewModel>,
    val zoning: List<SoftZoningConfig>
) : ExecutionData()

On top of this, keep in mind that the type JSON attribute must match the @Type name attribute. This means that it must match UpdateNetworkConfigurationsExecutionData. Your JSON must be something similar to:

"type": // ChangeRequestType, "data": { "type": "UpdateNetworkConfigurationsExecutionData", "updatedProfiles": [ // UpdateSalesChannelProfileViewModel objects "zoning": [ // SoftZoningConfig objects Brilliant! I did receive the type, but not in the ExecutionData, instead I received it in the ExecutionPayload. Is there a way I can annotate ExecutionPayload to tell jackson "hey, here's the type that you need, it's at this level, and not in the ExecutionData"? – shidoro Apr 9, 2022 at 12:14 I guess the issue is that the service I call doesn't know what type of ExecutionData it will be. It only knows the generic type of ExecutionPayload which is a NetworkConfiguration type. This is where I thought I'd be able to map it to any subclass of ExecutionData. My mistake was that I thought jackson would know the correct subtype from comparing the payloads. Anyway your answer got me unstuck. I have an idea on what I could do next. Thank you very much, João! – shidoro Apr 9, 2022 at 12:39

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.