@POST @Consumes("application/json") public Response create(Produto entity) { em.persist(entity); return Response.created( UriBuilder.fromResource(ProdutoEndpoint.class) .path(String.valueOf(entity.getId())) .build()) .build(); }
@PUT @Path("/{id:[0-9][0-9]*}") @Consumes("application/json") public Response update(@PathParam("id") Long id, Produto entity) { if (entity == null) { return Response.status(Status.BAD_REQUEST).build(); } if (!id.equals(entity.getId())) { return Response.status(Status.CONFLICT).entity(entity).build(); } if (em.find(Produto.class, id) == null) { return Response.status(Status.NOT_FOUND).build(); } try { entity = em.merge(entity); } catch (OptimisticLockException e) { return Response.status(Response.Status.CONFLICT).entity(e.getEntity()).build(); } return Response.noContent().build(); }