Jaxrs client

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jaxrs/jaxrs-client/
Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:
mvn test -Dtest=MyResourceTest

MyResourceTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment(testable = false)
public static WebArchive createDeployment() {
    return ShrinkWrap.create(WebArchive.class)
            .addClasses(
                    MyApplication.class, MyResource.class, People.class,
                    Person.class, PersonSessionBean.class);
}

Test of getList method, of class MyResource.

@Test
public void test1PostAndGet() {
    MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
    map.add("name", "Penny");
    map.add("age", "1");
    target.request().post(Entity.form(map));

    map.clear();
    map.add("name", "Leonard");
    map.add("age", "2");
    target.request().post(Entity.form(map));

    map.clear();
    map.add("name", "Sheldon");
    map.add("age", "3");
    target.request().post(Entity.form(map));

    Person[] list = target.request().get(Person[].class);
    assertEquals(3, list.length);

    assertEquals("Penny", list[0].getName());
    assertEquals(1, list[0].getAge());

    assertEquals("Leonard", list[1].getName());
    assertEquals(2, list[1].getAge());

    assertEquals("Sheldon", list[2].getName());
    assertEquals(3, list[2].getAge());
}

Test of getPerson method, of class MyResource.

@Test
public void test2GetSingle() {
    Person p = target
            .path("{id}")
            .resolveTemplate("id", "1")
            .request(MediaType.APPLICATION_XML)
            .get(Person.class);
    assertEquals("Leonard", p.getName());
    assertEquals(2, p.getAge());
}

Test of putToList method, of class MyResource.

@Test
public void test3Put() {
    MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
    map.add("name", "Howard");
    map.add("age", "4");
    target.request().post(Entity.form(map));

    Person[] list = target.request().get(Person[].class);
    assertEquals(4, list.length);

    assertEquals("Howard", list[3].getName());
    assertEquals(4, list[3].getAge());
}

Test of deleteFromList method, of class MyResource.

@Test
public void test4Delete() {
    target
            .path("{name}")
            .resolveTemplate("name", "Howard")
            .request()
            .delete();
    Person[] list = target.request().get(Person[].class);
    assertEquals(3, list.length);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void test5ClientSideNegotiation() {
    String json = target.request().accept(MediaType.APPLICATION_JSON).get(String.class);

    JsonReader reader = Json.createReader(new StringReader(json));
    JsonArray jsonArray = reader.readArray();
    assertEquals(1, jsonArray.getJsonObject(0).getInt("age"));
    assertEquals("Penny", jsonArray.getJsonObject(0).getString("name"));
    assertEquals(2, jsonArray.getJsonObject(1).getInt("age"));
    assertEquals("Leonard", jsonArray.getJsonObject(1).getString("name"));
    assertEquals(3, jsonArray.getJsonObject(2).getInt("age"));
    assertEquals("Sheldon", jsonArray.getJsonObject(2).getString("name"));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void test6DeleteAll() {
    Person[] list = target.request().get(Person[].class);
    for (Person p : list) {
        target
                .path("{name}")
                .resolveTemplate("name", p.getName())
                .request()
                .delete();
    }
    list = target.request().get(Person[].class);
    assertEquals(0, list.length);
}

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Dec 11, 2014: Fixed and improved tests for jaxrs client and singleton due to tomee failures by Roberto Cortez
  • Jul 15, 2014: Removed header license. the licensing is now referenced in the license file in the root of the project by Roberto Cortez
  • Apr 04, 2014: Removing redundant copyright by arun-gupta
  • Dec 03, 2013: Change to use client.target(uri) to be compatible with jax-rs 1.0 by Aslak Knutsen
  • Nov 21, 2013: Formatting changes by Arun Gupta
  • Nov 12, 2013: Convert jax-rs samples to arquillian: jax-rs client by Aslak Knutsen
  • Nov 08, 2013: Moved resteasy jaxb module to parent pom.xml by Arun Gupta
  • Nov 08, 2013: Fix test failures by Petr Sakař
  • Nov 08, 2013: Remove compile warnings by Petr Sakař
  • Nov 08, 2013: Bugfix - concurrentmodificationexception in personsessionbean deleteperson by Petr Sakař
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jaxrs/jaxrs-client/

Do the changes as you see fit and send a pull request!

Good Luck!