Jersey provides a client library which includes functions to request a resource. This tutorial show some basic requests from a client.
package my.second.rest; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import org.glassfish.jersey.client.ClientConfig; public class ClientRequest { public static void main(String[] args) { String HELLO_REST_URI = "http://localhost:8080/rest"; ClientConfig config = new ClientConfig(); Client client = ClientBuilder.newClient(config); WebTarget target = client.target(UriBuilder.fromUri(HELLO_REST_URI)).path("hello"); // Print service status //System.out.println(target.request(MediaType.TEXT_PLAIN).get().getStatus()); String response = target.request().accept(MediaType.TEXT_PLAIN).get(Response.class).toString(); System.out.println(response); // Print response as plain text System.out.println(target.request().accept(MediaType.TEXT_PLAIN).get(String.class)); // Print response as HTML System.out.println(target.request(MediaType.TEXT_HTML).get(String.class)); } }
InboundJaxrsResponse{context=ClientResponse{method=GET, uri=http://localhost:8080/rest/hello, status=200, reason=OK}} Hello world! <html> <title>Hello world!</title><body><h1>Hello world!</body></h1></html>