Search in sources :

Example 36 with GenericType

use of javax.ws.rs.core.GenericType in project cxf by apache.

the class JAXRSRxJava2FlowableTest method doTestGetHelloWorldJsonList.

private void doTestGetHelloWorldJsonList(String address) throws Exception {
    WebClient wc = WebClient.create(address, Collections.singletonList(new JacksonJsonProvider()));
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000);
    GenericType<List<HelloWorldBean>> genericResponseType = new GenericType<List<HelloWorldBean>>() {
    };
    List<HelloWorldBean> beans = wc.accept("application/json").get(genericResponseType);
    assertEquals(2, beans.size());
    assertEquals("Hello", beans.get(0).getGreeting());
    assertEquals("World", beans.get(0).getAudience());
    assertEquals("Ciao", beans.get(1).getGreeting());
    assertEquals("World", beans.get(1).getAudience());
}
Also used : GenericType(javax.ws.rs.core.GenericType) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) List(java.util.List) LinkedList(java.util.LinkedList) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 37 with GenericType

use of javax.ws.rs.core.GenericType in project quickstart by wildfly.

the class ContactsRestClient method requestResponseFiltersTest.

// Shows how to use Request and Response filters
public void requestResponseFiltersTest() {
    log.info("### Testing Request and Response Filters ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(REST_TARGET_URL).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Invoking create new contact using a ClientRequestFilter");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Contact persistedContact = ClientBuilder.newClient().register(SavedByClientRequestFilter.class).target(REST_TARGET_URL).request().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Assert.assertEquals("A contact should be persisted with savedBy", SavedByClientRequestFilter.USERNAME, persistedContact.getSavedBy());
    // 3 - Fetch all Contacts
    log.info("Invoking list all contacts using a ClientResponseFilter");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    List<Contact> allContacts = ClientBuilder.newClient().register(LogResponseFilter.class).target(REST_TARGET_URL).request().get(contactsListType);
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact)

Example 38 with GenericType

use of javax.ws.rs.core.GenericType in project quickstart by wildfly.

the class ContactsRestClient method delayedInvocationTest.

// Shows how to use a delayed REST invocation
public void delayedInvocationTest() throws Exception {
    log.info("### Testing Delayed invocaton ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(REST_TARGET_URL).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Creating a new contact invocation");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Invocation saveContactInvocation = ClientBuilder.newClient().target(REST_TARGET_URL).request().buildPost(Entity.entity(c, MediaType.APPLICATION_JSON));
    // 3 - Create a new list Contacts Invocation
    log.info("Creating list all contacts invocation");
    Invocation listContactsInvocation = ClientBuilder.newClient().target(REST_TARGET_URL).request().buildGet();
    // 4 - Synch Save contact
    log.info("invoking the new contact");
    Contact persistedContact = saveContactInvocation.invoke(Contact.class);
    Assert.assertEquals("A contacts should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 5 - Async List contacts
    log.info("invoking list all contacts ASYNC");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    Future<List<Contact>> futureAllContacts = listContactsInvocation.submit(contactsListType);
    List<Contact> allContacts = futureAllContacts.get();
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Invocation(javax.ws.rs.client.Invocation) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact)

Example 39 with GenericType

use of javax.ws.rs.core.GenericType in project quickstart by wildfly.

the class ContactsRestClientIT method requestResponseFiltersTest.

// Shows how to use Request and Response filters
@Test
public void requestResponseFiltersTest() {
    log.info("### Testing Request and Response Filters ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(getRequestUrl()).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Invoking create new contact using a ClientRequestFilter");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Contact persistedContact = ClientBuilder.newClient().register(SavedByClientRequestFilter.class).target(getRequestUrl()).request().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Assert.assertEquals("A contact should be persisted with savedBy", SavedByClientRequestFilter.USERNAME, persistedContact.getSavedBy());
    // 3 - Fetch all Contacts
    log.info("Invoking list all contacts using a ClientResponseFilter");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    List<Contact> allContacts = ClientBuilder.newClient().register(LogResponseFilter.class).target(getRequestUrl()).request().get(contactsListType);
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact) Test(org.junit.Test)

Example 40 with GenericType

use of javax.ws.rs.core.GenericType in project quickstart by wildfly.

the class ContactsRestClientIT method delayedInvocationTest.

// Shows how to use a delayed REST invocation
@Test
public void delayedInvocationTest() throws Exception {
    log.info("### Testing Delayed invocaton ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(getRequestUrl()).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Creating a new contact invocation");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Invocation saveContactInvocation = ClientBuilder.newClient().target(getRequestUrl()).request().buildPost(Entity.entity(c, MediaType.APPLICATION_JSON));
    // 3 - Create a new list Contacts Invocation
    log.info("Creating list all contacts invocation");
    Invocation listContactsInvocation = ClientBuilder.newClient().target(getRequestUrl()).request().buildGet();
    // 4 - Synch Save contact
    log.info("invoking the new contact");
    Contact persistedContact = saveContactInvocation.invoke(Contact.class);
    Assert.assertEquals("A contacts should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 5 - Async List contacts
    log.info("invoking list all contacts ASYNC");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    Future<List<Contact>> futureAllContacts = listContactsInvocation.submit(contactsListType);
    List<Contact> allContacts = futureAllContacts.get();
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Invocation(javax.ws.rs.client.Invocation) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact) Test(org.junit.Test)

Aggregations

GenericType (javax.ws.rs.core.GenericType)71 WebTarget (javax.ws.rs.client.WebTarget)42 List (java.util.List)35 Test (org.junit.Test)35 Response (javax.ws.rs.core.Response)24 ArrayList (java.util.ArrayList)15 GenericEntity (javax.ws.rs.core.GenericEntity)15 Collection (java.util.Collection)12 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 JerseyTest (org.glassfish.jersey.test.JerseyTest)9 Queue (java.util.Queue)8 GET (javax.ws.rs.GET)7 Produces (javax.ws.rs.Produces)7 MediaType (javax.ws.rs.core.MediaType)7 Type (java.lang.reflect.Type)6 HashSet (java.util.HashSet)6 ExecutionException (java.util.concurrent.ExecutionException)6 Path (javax.ws.rs.Path)6 JAXBElement (javax.xml.bind.JAXBElement)6 Comparator (java.util.Comparator)5