简体   繁体   中英

How can I retrieve an object by ID with dropwizard?

I'm trying to make a RESTful API with dropwizard, but, when I try to type the resource/id, it returns me an error 500, but when I try to return all objects created in the ArrayList, it goes ok.

@Path("/avioes")
@Produces(MediaType.APPLICATION_JSON)
public class AvioesResource {
    private AvioesDAO dao;

    public AvioesResource(AvioesDAO dao) {
        this.dao = dao;
    }

    @POST
    public Aviao create(Aviao aviao) {
        return dao.criar(aviao);
    }

    @GET
    public List<Aviao> read() {
        return dao.lerTodos();
    }

    @PUT
    @Path("{banana}")
    public Response update(@PathParam("banana") LongParam id, Aviao aviao) {
        if(dao.atualizar(id.get(), aviao)) {
            return Response.ok().build();
        }

        throw new WebApplicationException("Não rolou alterar", Response.Status.NOT_FOUND);
    }

    @DELETE
    @Path("{maca}")
    public Response delete(@PathParam("maca") LongParam id, Aviao aviao) {
        if(dao.apagar(id.get())) {
            return Response.ok().build();
        }

        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }

You don't have an API method mapped for getting a single resource. You'd need something like this:

@GET
@Path("{id}")
public Aviao read(@PathParam("id") LongParam id) {
    return dao.get(id);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM