Search in sources :

Example 26 with ParameterizedTypeReference

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());
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 27 with ParameterizedTypeReference

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);
    }
}
Also used : HashMap(java.util.HashMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) RequestEntity(org.springframework.http.RequestEntity) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 28 with ParameterizedTypeReference

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;
}
Also used : Traverson(org.springframework.hateoas.client.Traverson) CardResponse(model.banking.account.CardResponse) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Resources(org.springframework.hateoas.Resources) URI(java.net.URI)

Example 29 with ParameterizedTypeReference

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();
}
Also used : StandingOrder(model.banking.StandingOrder) Traverson(org.springframework.hateoas.client.Traverson) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Resources(org.springframework.hateoas.Resources) URI(java.net.URI)

Example 30 with ParameterizedTypeReference

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();
}
Also used : Traverson(org.springframework.hateoas.client.Traverson) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) URISyntaxException(java.net.URISyntaxException) Resources(org.springframework.hateoas.Resources) URI(java.net.URI)

Aggregations

ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)57 MediaType (org.springframework.http.MediaType)29 HttpHeaders (org.springframework.http.HttpHeaders)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)22 List (java.util.List)20 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)12 URI (java.net.URI)11 Test (org.junit.Test)10 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)8 URISyntaxException (java.net.URISyntaxException)7 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)6 HttpEntity (org.springframework.http.HttpEntity)6 Resources (org.springframework.hateoas.Resources)5 Traverson (org.springframework.hateoas.client.Traverson)5 IOException (java.io.IOException)4 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)4