GET methods

GET is the most used. It lets you accommodate the address data of the requested page, followed by the name of the page, a question mark, and the name/value pairs of data that interest us. Name and value are separated by an equals sign. The different name/value pairs are separated by &. So, imagine having the product.html page that shows the characteristics of a product passing the code and the category of the product itself. To show the A7 product data category 2, we will have to call up the page like this:

<a href="product.html?cod=a7&cat=2">

The string that is after the question mark, containing the names and values of the parameters, is called a query string. When the product.html page is called up in this way, it will be elaborated by a web server. So, to return to the example of the catalog, we can imagine having a page where we show a table with the name of each product on one line, and, next, the link that allows us to see the features of that product. In each row, then, this link always recalls the product.html page, valuing the different values of cod and cat each time.

Here's a sample of a client that calls a REST service through the GET method:

@ArquillianResource
private URL url;
...
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url + "services/calculator/sum?value=4&value=6");
Response response = target.request().get();
double value = response.readEntity(Double.class);
response.close();

The ClientBuilder is new in JAX-RS 2.0. Earlier, you were forced to write a client through implementation classes. The newClient method automatically finds the RESTEasy builder and returns a client to elaborate calls. You can see how the REST builder is declared in RESTEasy here:

public class ResteasyClientBuilder extends ClientBuilder
{ ...}

The REST builder extends the javax.ws.rs.client.ClientBuilder abstract class. As good practice in Java, JAX-RS 2.0 finds the builder searching inside the JAR libraries the file META-INF/services/javax.ws.rs.client.ClientBuilder. In the content RESTEasy, put the name of the implementation class:

org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder

In the target method, you specify the GET URL with the parameters. Then the get() method returns the result as the Response class. The result can be cast according to the declared class of the service. In this case, the sum returns a double value:

public double sum(@QueryParam("value") double... values)

In the URL, we send the parameters value declared through the @QueryParam annotation. In this case, we can send a chain of value parameters. They will be managed by the server as an array or a vararg.

Arquillian provides a function to obtain the current URL of the deployment package, simply injecting a URL field through the @ArquillianResource annotation.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.104.29