use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.
the class TestServlet method processRequest.
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JAX-RS 2 Client Invocation API</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>JAX-RS 2 Client Invocation API at " + request.getContextPath() + "</h1>");
out.println("Initializing client...<br>");
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/resource");
// GET
out.print("Building a GET request ...<br>");
Invocation i1 = target.request().buildGet();
out.print("GET request ready ...<br>");
// POST
out.print("Building a POST request...<br>");
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.add("name", "Name");
map.add("age", "17");
Invocation i2 = target.request().buildPost(Entity.form(map));
out.print("POSTed request ready...<br>");
Collection<Invocation> is = Arrays.asList(i1, i2);
for (Invocation i : is) {
i.invoke();
System.out.println("Request invoked");
}
out.println("... done.<br>");
out.println("</body>");
out.println("</html>");
}
use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.
the class MyResourceTest method setUp.
@Before
public void setUp() throws MalformedURLException {
Client client = ClientBuilder.newClient();
target = client.target(URI.create(new URL(base, "webresources/persons").toExternalForm()));
target.register(Person.class);
}
use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.
the class MyResourceTest method setupClass.
@Before
public void setupClass() throws MalformedURLException {
Client client = ClientBuilder.newClient();
target = client.target(URI.create(new URL(base, "webresources/fruit").toExternalForm()));
}
use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.
the class TestServlet method processRequest.
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Request Binding</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Request Binding</h1>");
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/persons");
out.print("GETTing @CookieParam ...<br>");
out.print(target.request().get(String.class));
out.print("<br><br>GETTing @MatrixParam ...<br>");
out.print(target.path("matrix").matrixParam("start", "5").matrixParam("end", "10").request().get(String.class));
out.print("<br><br>GETTing @Context ...<br>");
out.print(target.path("context").request().get(String.class));
out.println("<br>... done.<br>");
out.println("</body>");
out.println("</html>");
}
use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.
the class MyResourceTest method setUp.
@Before
public void setUp() throws MalformedURLException {
Client client = ClientBuilder.newClient();
target = client.target(URI.create(new URL(base, "webresources/persons").toExternalForm()));
}
Aggregations