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/order").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>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.Client in project jersey by jersey.
the class ExceptionTest method test500StatusCode.
@Test
public void test500StatusCode() {
startServer(ExceptionResource.class);
Client client = ClientBuilder.newClient();
WebTarget r = client.target(getUri().path("500").build());
assertEquals(500, r.request().get(Response.class).getStatus());
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class LifecycleListenerTest method testReload.
@Test
public void testReload() {
final ResourceConfig rc = new ResourceConfig(One.class);
Reloader reloader = new Reloader();
rc.registerInstances(reloader);
startServer(rc);
Client client = ClientBuilder.newClient();
WebTarget r = client.target(getUri().path("/").build());
assertEquals("one", r.path("one").request().get(String.class));
assertEquals(404, r.path("two").request().get(Response.class).getStatus());
// add Two resource
reloader.reload(new ResourceConfig(One.class, Two.class));
assertEquals("one", r.path("one").request().get(String.class));
assertEquals("two", r.path("two").request().get(String.class));
}
use of javax.ws.rs.client.Client 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));
Client client = ClientBuilder.newClient();
WebTarget r = client.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