Существует сторонняя API REST, который я не контролирую, которая обнаруживает конечную точку.{
"device": "sensor-01",
"temperature": 23.5
}
< /code>
Однако, если есть ошибка на стороне API, она все равно будет возвращать HTTP 200, с таким корпусом, как это: < /p>
{
"message": "An error occurred",
"code": 400
}
< /code>
Опять же, я не контролирую этот API и не могу изменить это поведение.public record GoodPojo(String device, float temperature) {
}
< /code>
public record BadPojo(String message, int code) {
}
< /code>
However, it seems there is nothing common between the two objects. It is hard to find a common object, a common parent between the two.
And for the REST call, I am doing this.
public String question() {
var result = restClient.get()
.uri("https://the-api.com")
.retrieve()
.body(GoodPojo.class);
return result.toString();
}
< /code>
This works for the good object, but the bad object cannot be converted like this.
Is there a way in Spring to do something like:
public String question() {
var result = restClient.get()
.uri("https://the-api.com")
.retrieve()
.body(GoodPojo.class | BadPojo.class); //convert into the first one, and if you cannot, convert into the second one
return result.toString();
}
< /code>
Without having to try to convert this into a good object, and catch the exception, if it fails, to convert to the other object manually?
Basically, how to convert the HTTP response into different types possible?
Подробнее здесь: https://stackoverflow.com/questions/797 ... ferent-typ
Spring RestClient, чтобы десериализовать (извлечь) между двумя совершенно разными типами ⇐ JAVA
Программисты JAVA общаются здесь
1758307864
Anonymous
Существует сторонняя API REST, который я не контролирую, которая обнаруживает конечную точку.{
"device": "sensor-01",
"temperature": 23.5
}
< /code>
Однако, если есть ошибка на стороне API, она все равно будет возвращать HTTP 200, с таким корпусом, как это: < /p>
{
"message": "An error occurred",
"code": 400
}
< /code>
Опять же, я не контролирую этот API и не могу изменить это поведение.public record GoodPojo(String device, float temperature) {
}
< /code>
public record BadPojo(String message, int code) {
}
< /code>
However, it seems there is nothing common between the two objects. It is hard to find a common object, a common parent between the two.
And for the REST call, I am doing this.
public String question() {
var result = restClient.get()
.uri("https://the-api.com")
.retrieve()
.body(GoodPojo.class);
return result.toString();
}
< /code>
This works for the good object, but the bad object cannot be converted like this.
Is there a way in Spring to do something like:
public String question() {
var result = restClient.get()
.uri("https://the-api.com")
.retrieve()
.body(GoodPojo.class | BadPojo.class); //convert into the first one, and if you cannot, convert into the second one
return result.toString();
}
< /code>
Without having to try to convert this into a good object, and catch the exception, if it fails, to convert to the other object manually?
Basically, how to convert the HTTP response into different types possible?
Подробнее здесь: [url]https://stackoverflow.com/questions/79749328/spring-restclient-to-deserialize-retrieve-between-two-completely-different-typ[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия