Похожие чаты

I am writing a REST API which accepts a JSON

as input. Spring Boot automatically converts it into a POJO. However, the JSON that I have has the following structure.

{
"strategy": "STRATEGY_NAME",
"data" : {}
}

The problem is, depending on the strategy, the data object will change. Any way to handle this?

2 ответов

26 просмотров

Your DTOs for each strategy have to extend from same base class, at least that "strategy" property. Otherwise you would end up writing your controller to accept Object param. So you have basically several options: - use @JsonComponent to register your custom codec, the most low-level solution - use jackson annotations, here's nice article I've just found, @JsonDeserialize and @JsonDeserialize are low-level variants of @JsonComponent - use jackson polymorphism support (again that nice article has it described https://www.baeldung.com/jackson-annotations#jackson-polymorphic-type-handling-annotations) - rethink your api design, and maybe you actually don't want dive into polymorphism; this may be the case if that number of strategies is huge, and/or there are not strict and clear boundaries between those strategies; in that case jackson has nice @JsonAnySetter which is exactly for this case

Well, to be less ambiguous. If you're 100% sure about your api, then in your exact case I would suggest using jackson polymorphic stuff (@JsonTypeInfo, @JsonSubType etc). Other options are nice to know, but polymorpic jackson annotations suit best for that particular case

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта