Похожие чаты

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 ответов

21 просмотр

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

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

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

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
any reference of this implementation?
BitBuddha
29
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
11
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
11
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Карта сайта