Примеры к занятию REST-клиенты, SOAP, Spring WebServices и клиенты к ним группа 2020-02. Работа с URI и получение массива

This commit is contained in:
kataus
2020-07-01 20:29:55 +03:00
parent 1b73f2dfbd
commit b93c86f260
@@ -2,11 +2,19 @@ package ru.otus.spring.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import ru.otus.spring.dto.Country;
import java.net.URI;
import java.util.List;
@Service
public class CountryServiceRest implements CountryService {
@@ -18,4 +26,21 @@ public class CountryServiceRest implements CountryService {
log.info("Request");
return rest.getForObject("https://restcountries.eu/rest/v2/alpha/" + id, Country.class);
}
@Override
public List<Country> getCountries(){
RequestEntity request = new RequestEntity( HttpMethod.GET
, UriComponentsBuilder.fromHttpUrl( "https://restcountries.eu" )
.path( "rest/v2" )
.queryParam( "locale", "ru" )
.build().toUri());
log.info( request.getUrl().toString() );
ResponseEntity<List<Country>> response =
rest.exchange( request,
new ParameterizedTypeReference<List<Country>>(){} );
return response.getBody();
}
}