简体   繁体   English

如何使用dropwizard通过ID检索对象?

[英]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. 我正在尝试使用dropwizard创建一个RESTful API,但是,当我尝试键入资源/ id时,它返回一个错误500,但是当我尝试返回在ArrayList中创建的所有对象时,一切正常。

@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. 您没有为获取单个资源而映射的API方法。 You'd need something like this: 您需要这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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