@Path("/users") public class UserResource { @GET @Path("/{id}") public User getUser(@PathParam("id") int id) { return user; } @POST public Response createUser(User user) { return Response.status(Response.Status.CREATED).build(); } } @Path("/books") public class BookResource { @GET @Path("/{id}") public Book getBook(@PathParam("id") int id) { return book; } @GET public List<Book> searchBooks(@QueryParam("keyword") String keyword) { return books; } } @Provider public class CustomExceptionMapper implements ExceptionMapper<CustomException> { @Override public Response toResponse(CustomException ex) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); } } @Path("/admin") @RolesAllowed("ADMIN") public class AdminResource { } public class UserResourceTest { @Test public void testGetUser() { WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/api/users/1"); Response response = target.request().get(); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); User user = response.readEntity(User.class); assertNotNull(user); response.close(); } }


上一篇:
下一篇:
切换中文