Jersey Client - Quiz

Total: 32 questions

1. 

The purpose of Jersey Client API.

The Jersey client API is a high-level Java based API for working with RESTful Web services.

2. 

Is Jersey Client API designed to work with JAX-RS services only?

It can be used to work with any RESTful Web service and is not restricted to services implemented by JAX-RS.

3. 

Steps to create the client API.

  1. Create an instance of a Client.
  2. Configure the Client if necessary.
  3. Create and configure the WebResource.
  4. Build a request.
  5. Receive a response.
4. 

How to create Client instance in Jersey Client API?

Client c = Client.create();

5. 

How a Client instance can be configured?

  1. By changing properties of the client.
  2. With a special setter methods.
  3. With a ClientConfig object.
6. 

How a WebResource can be created in Jersey Client API?

It can be created from a Client instance or from another WebResource.

7. 

Example of creating WebResource from the Client instance.

WebResource r = c.resource("http://localhost:8080/example1/resource");
8. Can a Client instance and WebResource instances be shared between multiple threads in Jersey Client API?
Yes.
9. Is it better to reuse Client instance for the creation of Web resources or create a new one each time?
Client instances are expensive resources. It is better to reuse a configured instance.
10. 

Does a WebResource instance inherit the configuration declared on the client instance?

Yes.

11. 

Example of sending a POST request with an entity, with an Accept header of application/json, application/xml and a non-standard header NewHeader of NewValue.

WebResource r =...
String request = "content";
String response = r.accept(
    MediaType.APPLICATION_JSON_TYPE,
    MediaType.APPLICATION_XML_TYPE).
    header("NewHeader", "NewValue").
    post(String.class, request);
12. 

Example of declaring Content-Type of the request entity in Jersey Client API.

String response = r.accept(
    MediaType.APPLICATION_JSON_TYPE,
    MediaType.APPLICATION_XML_TYPE).
    type(MediaType.TEXT_PLAIN_TYPE).
    post(String.class, request);
13. 

Example of declaring request entity and type by entity method.

String response = r.accept(
    MediaType.APPLICATION_JSON_TYPE,
    MediaType.APPLICATION_XML_TYPE).
    entity(request, MediaType.TEXT_PLAIN_TYPE).
    post(String.class);
14. How the Java type is declared if the response has an entity (or representation)?
It is declared in the terminating HTTP method. For example if returned Java type is String: String response = r.accept(...)...post(String.class);
15. 

Example of receiving entity tag and response entity from the response.

ClientResponse response = r.get(ClientResponse.class);
EntityTag e = response.getEntityTag();
String entity = response.getEntity(String.class);
16. When UniformInterfaceException is thrown?
If the ClientResponse type is not utilized and the response status is greater than or equal to 300.
17. 

Example of catching UniformInterfaceException and obtaining the ClientResponse.

try {
    String entity = r.get(String.class);
} catch (UniformInterfaceException ue) {
    ClientResponse response = ue.getResponse();
}
18. 

Example of creating new WebResource from an existing WebResource and adding some query parameters using MultivaluedMap.

WebResource r = c.resource("http://localhost:8080/example/exams");

MultivaluedMap<String, String> params = MultivaluedMapImpl();
params.add("name", "Helen");
params.add("age", "16");

String response = r.path("resource").
    queryParams(params).
    get(String.class);
19. How to process a response entity (or representation) as a stream of bytes?
InputStream is used: InputStream in = r.get(InputStream.class); // Read from the stream ... in.close();
20. How to POST a file in Jersey Client API?
Use File as follows: File f = ... String response = r.post(String.class, f);
21. 

Example of registering a provider class with Jersey Client API.

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(ExamReader.class);
Client c = Client.create(cc);
22. Example of registering an instance or singleton of provider by Jersey Client API.
ClientConfig cc = new DefaultClientConfig(); ExamReader reader = ... cc.getSingletons().add(reader); Client c = Client.create(cc);
23. 

Are the Java types and representations, which are supported by default on the Jersey server side for requests and responses, supported on the client side?

Yes.

24. 

Example of sending a GET request with an Accept header of application/json, application/xml and a non-standard header NewHeader of NewValue.

WebResource r =...
String response = r.accept(
    MediaType.APPLICATION_JSON_TYPE,
    MediaType.APPLICATION_XML_TYPE).
    header("NewHeader", "NewValue").
    get(String.class);
25. 

The purpose of adding filter to Jersey client?

It gives the possibility to read and modify requests and responses.

26. 

Are filters added to Client or WebResource instance?

It is possible to add filters to the Client and WebResource.

 

27. 

Does a WebResource inherit filters of its creator?

Yes.

28. 

In which order filters are applied for requests? 

For requests, filters are applied in reverse order, starting with the WebResource filters and then moving to the inherited filters.

29. 

In which order filters are applied for responses? 

For responses, filters are applied starting with inherited filters and followed by the filters added to the WebResource.

30. 

Which filters does the Jersey Client API support?

A GZIPContentEncodingFilter and a LoggingFilter.

31. 

What GZIPContentEncodingFilter is used for?

If this filter is added then a request entity is compressed with the Content-Encoding of gzip, and a response entity if compressed with a Content-Encoding of gzip is decompressed. The filter declares an Accept-Encoding of gzip.

32. 

What LoggingFilter is used for?

If this filter is added then the request and response headers as well as the entities are logged to a declared output stream if present, or to System.out if not. Often this filter will be placed at the end of the ordered list of filters to log the request before it is sent and the response after it is received.

Page 1 of 1