Enterprise Java Development@TOPIC@
determine the baseUrl for the application
String baseHttpUrlString = System.getProperty("url.base.http", "http://localhost:8080");
build a URL to the specific resource
URI resourceUrl = javax.ws.rs.core.UriBuilder.fromUri(baseHttpUrlString)
//.path("") //using root resource - no need for extra path
.build();
construct a JAX-RS client
javax.ws.rs.client.Client client = javax.ws.rs.client.ClientBuilder.newClient();
create the WebTarget to represent intended resource
javax.ws.rs.client.WebTarget target = client.target(resourceUrl);
create an overall request, indicating Acceptable response types
javax.ws.rs.client.Invocation.Builder request = target.request(MediaType.TEXT_HTML_TYPE);
create a method-specific request
javax.ws.rs.client.Invocation method = request.buildGet();
invoke the request and get an actual response from remote resource
javax.ws.rs.core.Response response = method.invoke();
interpret response
javax.ws.rs.core.Response.StatusType status = response.getStatusInfo();
int statusCode = response.getStatus();
assertEquals("unexpected statusType", Status.OK, status);
assertEquals("unexpected statusCode", 200, statusCode);
get response entity
String content = response.readEntity(String.class);
assertNotEquals("unexpected size", 0, content.length());
logger.debug("GET {} => {}/{}", target.getUri(), response.getStatus(), response.getStatusInfo());
GET http://localhost:8080 => 200/OK
Declare root URI using class that extends Application
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("api")
public class TodosApplication extends Application {
}
Root will be accessible under (WAR context-root)/api
Declare resource class and its path below root
import javax.ws.rs.Path;
@Path("greetings")
public class GreetingsResource {
Resource will be accessible under (WAR context-root)/api/greetings
Declare resource method
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
@GET
@Path("hi")
@Produces(MediaType.TEXT_PLAIN)
public Response sayHi() {
//...
}
Be sure to declare the method as "public" - or else it will quietly fail
Resource will be accessible under (WAR context-root)/api/greetings/hi
Method will be accessible using GET /api/greetings/hi
# client-side
URI uri = UriBuilder.fromUri(baseTodosUrl)
.path("greetings")
.path("hi")
.build();
WebTarget target = client.target(uri);
Method will be used for all GET URI callers that can Accept "text/plain" content
# client-side
Response response = target.request(MediaType.TEXT_PLAIN_TYPE)
.get();
Implement a response
public Response sayHi() {
String entity = "hi";
ResponseBuilder rb = Response.ok(entity);
return rb.build();
}
Response will return a 200/OK status code
# client-side
String greeting = response.readEntity(String.class);
logger.info("GET {} => {}", target.getUri(), response.getStatusInfo());
# client-side GET http://localhost:8080/ejavaTodos/api/greetings/hi => OK
Response will return a text String using "text/plain" content marshaling
# client-side
String greeting = response.readEntity(String.class);
logger.info("{}", greeting);
# client-side hi
JAX-RS API
<properties>
<javax.ws.rs-api.version>2.1</javax.ws.rs-api.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${javax.ws.rs-api.version}</version>
</dependency>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
Client-Side Provider
<properties>
<resteasy.version>3.5.1.Final</resteasy.version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>test</scope>
</dependency>
Additional dependencies will be required when adding structured content to payloads