X

Geertjan's Blog

  • June 21, 2014

RESTful Web Service Clients

Geertjan Wielenga
Product Manager

Here's a small snippet that's useful for hooking into the generated code for creating RESTful web service clients in NetBeans IDE. I.e., you start by exposing the database via RESTful web services, then you create a RESTful web service client, which gives you a new Java class with a bunch of methods for interacting with the web services.

Now, how do you print out all the, for example, customers via your RESTful web service client? Here's how, where "CustomerFacadeREST_JerseyClient" is the name of the generated RESTful web service client:

public static void main(String[] args) {
    CustomerFacadeREST_JerseyClient client = new CustomerFacadeREST_JerseyClient();
    GenericType<List<Customer>> gType = new GenericType<List<Customer>>() {};
    Response response = client.findAll_XML(Response.class);
    List<Customer> customers = response.readEntity(gType);
    for (Customer c : customers) {
        System.out.println(c.getCustomerId() + " / " + c.getName());
    }
}

And here's a step by step silent YouTube movie that shows you how to get to the place where the code snippet above can be used, i.e., how to expose a database via RESTful web services and consume them via a generated RESTful web service client:

Join the discussion

Comments ( 1 )
  • guest Thursday, April 20, 2017

    HiGeertjan Wielenga

    It was really useful for a POC, but now the issue I am facing is very different and I need you guidance to resolve.

    I do read entity like - Response response = client.findAll_JSON(Response.class);.

    It returns me the top level entity means the parent entity, and through that entity I can traverse to all the child entity defined as foreign key relationship in the database. But if I want to cascade update or insert the entities by passing the parent entity like

    UserLogin UL = new UserLogin();

    List<UserLogin> userLogin;

    UtilityController ULUC = new UtilityController(UL.getClass().getCanonicalName().toLowerCase()); // Instance for Jersy client with dynamic Entity name

    GenericType<UserLogin> gType = new GenericType<UserLogin>() {};

    Response response = ULUC.find_JSON(Response.class,"1");

    UserLogin userLogin = response.readEntity(gType); // It is providing me Parent Child structure from JPA

    System.out.println(c.getLoginUsername() + " / " + c.getIdCGHSReg().getBankACNo()+" / "+c.getIdCGHSReg().getIdBankMaster().getBankName());

    userLogin.getIdCGHSReg().setCity("Gurgaon"); //Here I want to set the data into Child Entity

    ULUC.create_JSON(userLogin); // Here I am passing the Parent object having child entity

    I seek you help, as I am unable to insert the child facing constraint violation error.

    Thank you sir

    Abdul


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.