Надеюсь, это поможет. Вы можете сделать это с помощью аннотаций Spring RestTemplate.getForObject и Jackson.
Это класс для ответа Twitter:
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class TwitterResponse {
private List<TwitterId> ids;
@JsonCreator
public TwitterResponse(List<TwitterId> ids) {
this.ids = ids;
}
public List<TwitterId> getIds() {
return ids;
}
@Override
public String toString() {
return "TwitterResponse [ids=" + ids + "]";
}
}
Это класс TwitterId:
import com.fasterxml.jackson.annotation.JsonProperty;
public class TwitterId {
private String twitterId;
public TwitterId(String twitterId) {
this.twitterId = twitterId;
}
public String getTwitterId() {
return twitterId;
}
@Override
public String toString() {
return "TwitterId [twitterId=" + twitterId + "]";
}
}
И простой модульный тест, показывающий, что он работает. Я только что создал простой контроллер, который возвращает ваши данные выше, и я тестирую его, используя RestTemplate
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
public class ControllerTest {
private static final Logger log = LoggerFactory.getLogger(ControllerTest.class);
@Test
public void testTwitter() {
try {
RestTemplate restTemplate = new RestTemplate();
TwitterResponse twitterResponse =
restTemplate.getForObject("http://localhost:8080/twitter",
TwitterResponse.class);
log.debug("twitterResponse = {}", twitterResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
}
И на консоли отображается результат теста:
19:57:42.506 [main] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8080/twitter"
19:57:42.545 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
19:57:42.558 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:8080/twitter" resulted in 200 (OK)
19:57:42.559 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class org.porthos.simple.TwitterResponse] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@4b5a5ed1]
19:57:42.572 [main] DEBUG org.porthos.simple.ControllerTest - twitterResponse = TwitterResponse [ids=[TwitterId [twitterId=243439460], TwitterId [twitterId=13334762], TwitterId [twitterId=14654522]]]