com.github.daniel.shuy.ws.rs.jpa.crud.ResourceCRUD Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jax-rs-jpa-crud Show documentation
Show all versions of jax-rs-jpa-crud Show documentation
Provides extendable classes to create JAX-RS CRUD Web Services tied to JPA entity classes
package com.github.daniel.shuy.ws.rs.jpa.crud;
import java.io.Closeable;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* Extend this class to create a CRUD JAX-RS Repository Class.
*
* @param The Entity Class type
*/
public interface ResourceCRUD extends Closeable {
public abstract RepositoryCRUD getRepository();
@POST
@Consumes(MediaType.APPLICATION_JSON)
public default void create(E content) {
try {
getRepository().add(content);
}
finally {
close();
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public default List readAll() {
try {
return getRepository().getAll();
}
finally {
close();
}
}
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public default Response read(@PathParam("id") String id) {
try {
return doWithID(id, (idLong) -> {
return Response.ok(getRepository().get(idLong)).build();
});
}
finally {
close();
}
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public default void update(E content) {
try {
getRepository().update(content);
}
finally {
close();
}
}
@DELETE
@Path("{id}")
public default void delete(@PathParam("id") String id) {
try {
doWithID(id, (idLong) -> {
getRepository().remove(idLong);
});
}
finally {
close();
}
}
@Override
public default void close() {}
// TODO: change to private in Java 9
public default Response doWithID(String id, Function function) {
long idLong;
try {
idLong = Long.parseLong(id);
} catch (NumberFormatException e) {
return Response.status(Response.Status.BAD_REQUEST).entity("id must be a Number").build();
}
return function.apply(idLong);
}
// TODO: change to private in Java 9
public default void doWithID(String id, Consumer consumer) {
doWithID(id, (idLong) -> {
consumer.accept(idLong);
return null;
});
}
}