Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Samples
|
Rest Client HTTP (WebTarget)
Notes
Maven dependencies
GET Method
POST Method (url parameters)
POST Method (form parameters)
Notes
See this page for an example of a REST application:
JAX-RS: Sample Application (Jersey, Spring, Tomcat)
Maven dependencies
<dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.29.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>2.29.1</version> </dependency>
GET Method
Java example:
import java.io.IOException; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.logging.LoggingFeature; public class Test { public static void main(String[] args) throws IOException { final String urlString = "http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/getMyEntity1"; final Client client = ClientBuilder.newBuilder() .property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT, LoggingFeature.Verbosity.PAYLOAD_ANY) .property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_CLIENT, "INFO") .build(); WebTarget webTarget = client.target(urlString); webTarget = webTarget.queryParam("qpvalue1", "val11").queryParam("qpvalue2", "val21").queryParam("qpvalue3", "val31"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML); final Response response = invocationBuilder.get(); // response body //System.out.println(response.readEntity(String.class)); } }
Sample JAX-RS GET resource:
@GET @Path("/getMyEntity1") public Response getMyEntity1(@QueryParam(value = "qpvalue1") final String value1, @QueryParam(value = "qpvalue2") final String value2, @QueryParam(value = "qpvalue3") final String value3) { MyEntity myEntity = new MyEntity(); //... return Response.ok(myEntity).build(); }
Request/Response:
INFO: 1 * Sending client request on thread main 1 > GET http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/getMyEntity1?qpvalue1=val11&qpvalue2=val21&qpvalue3=val31 1 > Accept: application/xml
INFO: 1 * Client response received on thread main 1 < 200 1 < Content-Length: 688 1 < Content-Type: application/xml 1 < Date: Mon, 21 Oct 2018 00:14:43 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?><myEntity ...
POST Method (url parameters)
Java example:
import java.io.IOException; import javax.ws.rs.HttpMethod; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.logging.LoggingFeature; public class Test { public static void main(String[] args) throws IOException { final String urlString = "http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/postMyEntity1"; final Client client = ClientBuilder.newBuilder() .property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT, LoggingFeature.Verbosity.PAYLOAD_ANY) .property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_CLIENT, "INFO") .build(); WebTarget webTarget = client.target(urlString); webTarget = webTarget.queryParam("qpvalue1", "val11").queryParam("qpvalue2", "val21").queryParam("qpvalue3", "val31"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML); final Response response = invocationBuilder.method(HttpMethod.POST, null, Response.class); // response body //System.out.println(response.readEntity(String.class)); } }
Sample JAX-RS POST resource:
@POST @Path("/postMyEntity1") public Response postMyEntity1(@QueryParam(value = "qpvalue1") final String value1, @QueryParam(value = "qpvalue2") final String value2, @QueryParam(value = "qpvalue3") final String value3) { MyEntity myEntity = new MyEntity(); //... return Response.ok(myEntity).build(); }
Request/Response:
INFO: 1 * Sending client request on thread main 1 > POST http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/postMyEntity1?qpvalue1=val11&qpvalue2=val21&qpvalue3=val31 1 > Accept: application/xml
INFO: 1 * Client response received on thread main 1 < 200 1 < Content-Length: 902 1 < Content-Type: application/xml 1 < Date: Mon, 21 Oct 2018 00:22:36 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?><myEntity ...
POST Method (form parameters)
Java example:
import java.io.IOException; import javax.ws.rs.HttpMethod; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.logging.LoggingFeature; public class Test { public static void main(String[] args) throws IOException { final String urlString = "http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/postMyEntity2"; final Client client = ClientBuilder.newBuilder() .property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT, LoggingFeature.Verbosity.PAYLOAD_ANY) .property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_CLIENT, "INFO") .build(); WebTarget webTarget = client.target(urlString); webTarget = webTarget.queryParam("qpvalue1", "val11").queryParam("qpvalue2", "val21").queryParam("qpvalue3", "val31"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML); final Response response = invocationBuilder.method(HttpMethod.POST, Entity.entity(null, MediaType.APPLICATION_FORM_URLENCODED), Response.class); // response body //System.out.println(response.readEntity(String.class)); } }
Sample JAX-RS POST resource:
@POST @Path("/postMyEntity2") @Consumes({ MediaType.APPLICATION_FORM_URLENCODED }) public Response postMyEntity2(@FormParam(value = "qpvalue1") final String value1, @FormParam(value = "qpvalue2") final String value2, @FormParam(value = "qpvalue3") final String value3) { MyEntity myEntity = new MyEntity(); //... return Response.ok(myEntity).build(); }
Request/Response:
INFO: 1 * Sending client request on thread main 1 > POST http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/postMyEntity2?qpvalue1=val11&qpvalue2=val21&qpvalue3=val31 1 > Accept: application/xml 1 > Content-Type: application/x-www-form-urlencoded
INFO: 1 * Client response received on thread main 1 < 200 1 < Content-Length: 891 1 < Content-Type: application/xml 1 < Date: Mon, 21 Oct 2018 00:24:56 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?><myEntity ...
© 2010-2022
mti
tek