-2
I’m trying to popular a DTO through a received Json, in case I’m the Gson library, but I’m having trouble filling the field urlPhoto of my class hotelDTO. I can even convert Json to a Hoteldto object, but urlPhoto is null.
Json received:
{
"location_id": "306267",
"name": "EZ Aclimacao Hotel",
"latitude": "-23.5771",
"longitude": "-46.636818",
"num_reviews": "900",
"timezone": "America/Sao_Paulo",
"location_string": "Sao Paulo, State of Sao Paulo",
"photo": {
"images": {
"small": {
"width": "150",
"url": "https://media-cdn.tripadvisor.com/media/photo-l/19/62/7d/2d/fachada-do-ez.jpg",
"height": "150"
},
"thumbnail": {
"width": "50",
"url": "https://media-cdn.tripadvisor.com/media/photo-t/19/62/7d/2d/fachada-do-ez.jpg",
"height": "50"
},
"original": {
"width": "2900",
"url": "https://media-cdn.tripadvisor.com/media/photo-o/19/62/7d/2d/fachada-do-ez.jpg",
"height": "1933"
},
"large": {
"width": "550",
"url": "https://media-cdn.tripadvisor.com/media/photo-s/19/62/7d/2d/fachada-do-ez.jpg",
"height": "367"
},
"medium": {
"width": "250",
"url": "https://media-cdn.tripadvisor.com/media/photo-f/19/62/7d/2d/fachada-do-ez.jpg",
"height": "167"
}
}
}
In this case I would like to take the value "url": "https://media-cdn.tripadvisor.com/media/photo-o/19/62/7d/2d/fachada-do-ez.jpg", which is in the following path, "photo" -> "images" -> "origianl" -> "url"
My Class DTO:
public class HotelDTO {
@SerializedName("location_id")
private String locantionId;
@SerializedName("name")
private String hotelName;
@SerializedName("num_reviews")
private int numReview;
@SerializedName("location_string")
private String location;
@SerializedName("url")
private Map<String, Map<String, Map<String, String>>> urlPhoto = new HashMap<String, Map<String, Map<String, String>>>();
@Override
public String toString() {
return "HotelDTO{" +
"locantionId='" + locantionId + '\'' +
", hotelName='" + hotelName + '\'' +
", numReview=" + numReview +
", location='" + location + '\'' +
", urlPhoto=" + urlPhoto +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HotelDTO hotelDTO = (HotelDTO) o;
return numReview == hotelDTO.numReview &&
Objects.equals(locantionId, hotelDTO.locantionId) &&
Objects.equals(hotelName, hotelDTO.hotelName) &&
Objects.equals(location, hotelDTO.location) &&
Objects.equals(urlPhoto, hotelDTO.urlPhoto);
}
@Override
public int hashCode() {
return Objects.hash(locantionId, hotelName, numReview, location, urlPhoto);
}
public String getLocantionId() {
return locantionId;
}
public void setLocantionId(String locantionId) {
this.locantionId = locantionId;
}
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public int getNumReview() {
return numReview;
}
public void setNumReview(int numReview) {
this.numReview = numReview;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Map<String, Map<String, Map<String, String>>> getUrlPhoto() {
return urlPhoto;
}
public void setUrlPhoto(Map<String, Map<String, Map<String, String>>> urlPhoto) {
this.urlPhoto = urlPhoto;
}
}
My Controller:
@Api(tags = "HotelEndpointTest")
@RestController
@RequestMapping("/api/hotelstest")
public class HotelsControllerTest {
@RequestMapping(
method = RequestMethod.GET,
produces = {"application/json", "application/xml", "application/x-yaml"})
public ResponseEntity<String> sendHotels(@RequestParam(value = "location_id", defaultValue = "303631") String locationId,
@RequestParam(value = "adults", defaultValue = "1") String adults,
@RequestParam(value = "checkin", defaultValue = "2020-08-10") String checkin,
@RequestParam(value = "rooms", defaultValue = "1") String rooms,
@RequestParam(value = "nights", defaultValue = "2") String nights,
@RequestParam(value = "zff", defaultValue = "") String zff,
@RequestParam(value = "pricesmin", defaultValue = "") String pricesmin,
@RequestParam(value = "offset", defaultValue = "") String offset,
@RequestParam(value = "subcategory", defaultValue = "") String subcategory,
@RequestParam(value = "pricesmax", defaultValue = "") String pricesmax,
@RequestParam(value = "hotel_class", defaultValue = "") String hotel_class,
@RequestParam(value = "currency", defaultValue = "BRL") String currency,
@RequestParam(value = "amenities", defaultValue = "") String amenities,
@RequestParam(value = "child_rm_ages", defaultValue = "") String child_rm_ages,
@RequestParam(value = "limit", defaultValue = "") String limit,
@RequestParam(value = "order", defaultValue = "") String order,
@RequestParam(value = "lang", defaultValue = "pt_BR") String lang,
@RequestParam(value = "sort", defaultValue = "recommended") String sort) {
final RestTemplate restTemplate = new RestTemplate();
final String url = "https://tripadvisor1.p.rapidapi.com/hotels/list?zff=" + zff + "&pricesmin=" + pricesmin + "&offset=" + offset +
"&subcategory=" + subcategory + "&pricesmax=" + pricesmax + "&hotel_class=" + hotel_class + "¤cy=" + hotel_class + "&amenities=" + amenities + "+&child_rm_ages=" + child_rm_ages + "&" +
"limit=" + limit + "&order=" + order + "&lang=+" + lang + "&sort=" + sort + "&location_id=" + locationId + "&adults=" + adults + "&checkin=+" + checkin + "&rooms=" + rooms + "&nights=" + rooms;
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-rapidapi-host", "tripadvisor1.p.rapidapi.com");
headers.set("x-rapidapi-key", "MEU_TOKEN");
final HttpEntity httpEntity = new HttpEntity(headers);
final ResponseEntity<String> responseEntity = restTemplate.exchange(
url,
HttpMethod.GET,
httpEntity,
String.class
);
JsonReader jsonReader = Json.createReader(new StringReader(responseEntity.getBody()));
JsonObject jsonObject = jsonReader.readObject();
Gson gson = new Gson();
JsonArray jsonArray = jsonObject.get("data").asJsonArray();;
List<HotelDTO> hotelDTO = new ArrayList<>();
for (int i = 0; i < jsonArray.size() ; i++) {
jsonObject = jsonArray.getJsonObject(i);
hotelDTO.add(gson.fromJson(jsonObject.toString(), HotelDTO.class));
}
System.out.println(hotelDTO.size());
return responseEntity;
}
}
Hi Gustavo! Thanks for the help, I found the proposal of this resolution very cool! I confess that I am a beginner in this matter of communication of Microservices, I had not passed for my head this solution! Hugs
– Imai