use of javax.ws.rs.client.WebTarget in project javaee7-samples by javaee-samples.
the class MyResourceTest method testRequestWithAllParams.
@Test
public void testRequestWithAllParams() {
WebTarget t = target.path("/123").path("/abc").queryParam("param1", "foo").queryParam("param2", "bar").queryParam("param3", "baz");
String r = t.request().get(String.class);
assertEquals("/123/abc?param1=foo¶m2=bar¶m3=baz", r);
}
use of javax.ws.rs.client.WebTarget 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.WebTarget 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.WebTarget 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 Reader/Writer w/ JSON</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>JAX-RS Reader/Writer w/ JSON</h1>");
Client client = ClientBuilder.newClient();
client.register(MyReader.class).register(MyWriter.class);
WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/endpoint");
out.println("POST request");
MyObject mo = target.request().post(Entity.entity(new MyObject("Duke", 18), MediaType.APPLICATION_JSON), MyObject.class);
out.println("Received response: " + mo.getName() + ", " + mo.getAge() + "<br><br>");
out.println("Message exchanged using application/json type.");
out.println("</body>");
out.println("</html>");
}
use of javax.ws.rs.client.WebTarget in project jersey by jersey.
the class LifecycleListenerTest method testStartupShutdownHooks.
@Test
public void testStartupShutdownHooks() {
final StartStopListener listener = new StartStopListener();
startServer(new ResourceConfig(One.class).register(listener));
WebTarget r = ClientBuilder.newClient().target(getUri().path("/").build());
assertThat(r.path("one").request().get(String.class), equalTo("one"));
assertThat(r.path("two").request().get(Response.class).getStatus(), equalTo(404));
stopServer();
assertTrue("ContainerLifecycleListener.onStartup has not been called.", listener.started);
assertTrue("ContainerLifecycleListener.onShutdown has not been called.", listener.stopped);
}
Aggregations