use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.
the class GsonHttpMessageConverterTests method readParameterizedType.
@Test
@SuppressWarnings("unchecked")
public void readParameterizedType() throws IOException {
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {
};
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
assertEquals(1, results.size());
MyBean result = results.get(0);
assertEquals("Foo", result.getString());
assertEquals(42, result.getNumber());
assertEquals(42F, result.getFraction(), 0F);
assertArrayEquals(new String[] { "Foo", "Bar" }, result.getArray());
assertTrue(result.isBool());
assertArrayEquals(new byte[] { 0x1, 0x2 }, result.getBytes());
}
use of org.springframework.core.ParameterizedTypeReference in project java-chassis by ServiceComb.
the class TestRestTemplateWrapper method testRestTemplateWrapper.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRestTemplateWrapper() {
try {
MockUtil.getInstance().mockConsumerProviderManager();
MockUtil.getInstance().mockRegisterManager();
MockUtil.getInstance().mockServicePathManager();
MockUtil.getInstance().mockOperationLocator();
MockUtil.getInstance().mockOperationMeta();
MockUtil.getInstance().mockSchemaMeta();
MockUtil.getInstance().mockSchemaUtils();
MockUtil.getInstance().mockBeanUtils();
MockUtil.getInstance().mockRequestMeta();
MockUtil.getInstance().mockInvokerUtils();
RestTemplateWrapper lRestTemplateWrapper = (RestTemplateWrapper) RestTemplateBuilder.create();
lRestTemplateWrapper.put(URI.create("cse://test"), new Object());
lRestTemplateWrapper.delete(URI.create("cse://test"));
lRestTemplateWrapper.put("cse://test", new Object());
lRestTemplateWrapper.getForObject("cse://test", new Object().getClass());
lRestTemplateWrapper.getForEntity("cse://test", new Object().getClass());
lRestTemplateWrapper.postForObject("cse://test", new Object(), new Object().getClass());
lRestTemplateWrapper.postForEntity("cse://test", new Object(), new Object().getClass());
lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass());
lRestTemplateWrapper.delete("cse://test", new Object());
lRestTemplateWrapper.delete("cse://test", new HashMap<>());
Map<String, ?> uriVariables = new HashMap<>();
lRestTemplateWrapper.getForObject("cse://test", new Object().getClass(), uriVariables);
lRestTemplateWrapper.getForEntity("cse://test", new Object().getClass(), uriVariables);
lRestTemplateWrapper.getForObject(URI.create("cse://test"), new Object().getClass());
lRestTemplateWrapper.getForEntity(URI.create("cse://test"), new Object().getClass());
lRestTemplateWrapper.postForObject("cse://test", new Object(), new Object().getClass(), uriVariables);
lRestTemplateWrapper.postForObject(URI.create("cse://test"), new Object(), new Object().getClass());
lRestTemplateWrapper.postForEntity("cse://test", new Object(), new Object().getClass(), uriVariables);
lRestTemplateWrapper.postForEntity(URI.create("cse://test"), new Object(), new Object().getClass());
lRestTemplateWrapper.put("cse://test", new Object(), uriVariables);
lRestTemplateWrapper.put(URI.create("cse://test"), new Object());
lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass(), uriVariables);
ParameterizedTypeReference instance = Mockito.mock(ParameterizedTypeReference.class);
lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, instance, uriVariables);
lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, instance, new Object().getClass());
RequestEntity requestEntity = new RequestEntity<>(HttpMethod.GET, URI.create("cse://test"));
lRestTemplateWrapper.exchange(requestEntity, new Object().getClass());
lRestTemplateWrapper.exchange(requestEntity, instance);
lRestTemplateWrapper.exchange(URI.create("cse://test"), HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass());
lRestTemplateWrapper.exchange(URI.create("cse://test"), HttpMethod.GET, HttpEntity.EMPTY, instance);
Assert.assertTrue(RestTemplateBuilder.create().getClass().isAssignableFrom(RestTemplateWrapper.class));
} catch (Exception e) {
Assert.assertNotNull(e);
}
}
use of org.springframework.core.ParameterizedTypeReference in project amos-ss17-alexa by c-i-ber.
the class AccountFactory method getCardsForAccount.
/**
* Get all cards (credit / debit) for the given account
* @param number Account number
* @return Collection of CardResponses
* @throws URISyntaxException
* @throws HttpClientErrorException
*/
public Collection<CardResponse> getCardsForAccount(String number) throws HttpClientErrorException {
Traverson traverson = null;
try {
traverson = new Traverson(new URI(BankingRESTClient.BANKING_API_ENDPOINT + BankingRESTClient.BANKING_API_BASEURL_V1 + "/accounts/" + number + "/cards"), MediaTypes.HAL_JSON);
} catch (URISyntaxException e) {
e.printStackTrace();
}
ParameterizedTypeReference<Resources<CardResponse>> typeRefDevices = new ParameterizedTypeReference<Resources<CardResponse>>() {
};
Resources<CardResponse> resCardResponses = traverson.follow(rel("$._links.self.href")).toObject(typeRefDevices);
Collection<CardResponse> cards = resCardResponses.getContent();
return cards;
}
use of org.springframework.core.ParameterizedTypeReference in project amos-ss17-alexa by c-i-ber.
the class AccountAPI method getStandingOrdersForAccount.
/**
* Get all standing orders for the given account.
*
* @param accountNumber Account number
* @return Collection of StandingOrders
* @throws HttpClientErrorException
*/
public static Collection<StandingOrder> getStandingOrdersForAccount(String accountNumber) throws HttpClientErrorException {
// TODO: Create a generic method for getting embedded JSON-HAL collections (in BankingRESTClient)
Traverson traverson = null;
try {
traverson = new Traverson(new URI(BankingRESTClient.BANKING_API_ENDPOINT + BankingRESTClient.BANKING_API_BASEURL_V2 + "/accounts/" + accountNumber + "/standingorders"), MediaTypes.HAL_JSON);
} catch (URISyntaxException e) {
log.error("getStandingOrdersForAccount failed", e);
return null;
}
ParameterizedTypeReference<Resources<StandingOrder>> typeRefDevices = new ParameterizedTypeReference<Resources<StandingOrder>>() {
};
Resources<StandingOrder> resResponses = traverson.follow(rel("$._links.self.href")).withHeaders(bankingRESTClient.generateHttpHeaders()).toObject(typeRefDevices);
return resResponses.getContent();
}
use of org.springframework.core.ParameterizedTypeReference in project amos-ss17-alexa by c-i-ber.
the class SecuritiesAccountAPI method getSecuritiesForAccount.
/**
* Get all securities for the given securities account.
*
* @param securitiesAccountNumber Account number
* @return Collection of securities
* @throws HttpClientErrorException
*/
public static Collection<Security> getSecuritiesForAccount(Number securitiesAccountNumber) throws HttpClientErrorException {
// TODO: Create a generic method for getting embedded JSON-HAL collections (in BankingRESTClient)
Traverson traverson = null;
try {
traverson = new Traverson(new URI(BankingRESTClient.BANKING_API_ENDPOINT + BankingRESTClient.BANKING_API_BASEURL_V2 + "/securitiesAccounts/" + securitiesAccountNumber), MediaTypes.HAL_JSON);
} catch (URISyntaxException e) {
LOGGER.error("getSecuritiesForAccount failed", e);
return null;
}
ParameterizedTypeReference<Resources<Security>> typeRefDevices = new ParameterizedTypeReference<Resources<Security>>() {
};
Resources<Security> resResponses = traverson.follow(rel("$._links.self.href")).withHeaders(bankingRESTClient.generateHttpHeaders()).toObject(typeRefDevices);
return resResponses.getContent();
}
Aggregations