uCosminexus Application Server, Web Service Development Guide
This subsection describes a basic use case of a client API for RESTful Web Services. You can use any of the following methods to call a Web resource:
The description of each method is as follows:
The following figure shows a use case describing the sending and receiving of HTTP requests and HTTP responses by specifying a Java type.
Figure 11-1 Use case describing the sending and receiving of HTTP requests and HTTP responses by specifying a Java type
The coding example corresponding to figure 11-1 is as follows:
| Client client = Client.create(); Map<String, Object> properties = client.getProperties(); properties.put(ClientConfig.PROPERTY_READ_TIMEOUT, 10000); WebResource proxy = client.resource( "http://example.org/helloworld" ); WebResource.Builder builder = proxy.accept( MediaType.APPLICATION_JSON_TYPE ); builder = builder.type( MediaType.TEXT_PLAIN_TYPE ); String response = builder.post( String.class, "Some Request" ); |
The procedure in the example is as follows:
You can also receive an HTTP response in a generic type such as the ClientResponse object. Acquiring an HTTP response with the ClientResponse object enables a user to obtain various types of information of the received HTTP response (HTTP header, entity body, and status code).
You can acquire an entity body by using the getEntity() method, thereby specifying the Java type. For details on the methods supported by the ClientResponse class, see 25.1 The support range of client API interfaces and classes.
The method to acquire an HTTP response to be received with ClientResponse object is as follows:
To create an HTTP request by using the WebResource object, specify ClientResponse.class in the parameter that specifies the Java type of the HTTP response. Specify ClientResponse.class instead of String.class in the coding example described in 11.4.1(1) Sending and receiving HTTP requests and HTTP responses by specifying a Java type..
The HTTP response is always ClientResponse if you directly send an HTTP request using the Client class. For details on the Client class, see 11.4.1(3) Sending and receiving HTTP requests and HTTP responses in a generic type.
The following figure shows a use case describing sending and receiving HTTP requests and HTTP responses (ClientRequest and ClientResponse) in generic types.
Figure 11-2 Use case describing sending and receiving HTTP requests and HTTP responses in generic types.
The coding example corresponding to figure11-2 is as follows.
Client client = Client.create();
Map<String, Object> properties = client.getProperties();
properties.put(ClientConfig.PROPERTY_READ_TIMEOUT, 10000);
ClientRequest ro;
ClientRequest.Builder builder = ClientRequest.create();
builder.accept( MediaType.APPLICATION_JSON_TYPE );
builder.type( MediaType.TEXT_PLAIN_TYPE );
builder.entity("Some Request");
ro = builder.build(new URI("http://example.org/helloworld"), "POST");
ClientResponse clientResponse = client.handle(ro);
//The actual response in the form of String
String response = clientResponse.getEntity(String.class);
|
The procedure in the example is as follows:
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.