All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.panchitoboy.common.rest.Resource Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package com.github.panchitoboy.common.rest;

import com.github.panchitoboy.common.ecb.Boundary;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
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;

public abstract class Resource {

    @Inject
    Boundary boundary;

    protected Boundary getBoundary() {
        return boundary;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response list() throws IOException {
        List list = getBoundary().findAll();
        return Response.status(200).entity(list).build();
    }

    @GET
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(@PathParam("id") long id) throws IOException {
        T element = getBoundary().find(id);
        return Response.status(200).entity(element).build();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response create(T instance) throws IOException {
        getBoundary().create(instance);
        return Response.status(200).entity(instance).build();
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response update(T instance) throws IOException {
        getBoundary().update(instance);
        return Response.status(200).entity(instance).build();
    }

    @Path("{id}")
    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response remove(@PathParam("id") long id) {
        getBoundary().remove(id);
        return Response.status(200).build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy