我们如何使用dropwizard Web服务实现删除操作

时间:2015-01-09 10:39:27

标签: java jersey dropwizard

我阅读了许多博客和教程,但我没有在dropwizard Web服务中找到删除操作。

这是我的代码:

public class PersonDAO extends AbstractDAO<Person> {
    public PersonDAO(SessionFactory factory) {
        super(factory);
    }
    public Optional<Person> findById(Long id) {
        return Optional.fromNullable(get(id));
    }

    public Person create(Person person) {
        return persist(person);
    }

    public List<Person> findAll() {
        return list(namedQuery("com.example.helloworld.core.Person.findAll"));
    }
    //here i want to create delete method
}

这是我的服务文件:

@Path("/people")
@Produces(MediaType.APPLICATION_JSON)
public class PeopleResource {

    private final PersonDAO peopleDAO;

    public PeopleResource(PersonDAO peopleDAO) {
        this.peopleDAO = peopleDAO;
    }

    @POST
    @UnitOfWork
    public Person createPerson(Person person) {
        return peopleDAO.create(person);
    }

    @GET
    @UnitOfWork
    public List<Person> listPeople() {
        return peopleDAO.findAll();
    }

    @GET
    @Path("/{id}")
    public Optional<Person> getUser(@PathParam("id") Integer id) {
        Optional<Person> person = peopleDAO.findPeopleById(id);
        return person;
    }
    //here i add to delete method that call to dao class
}

如果有人有想法,请提前告知我。

2 个答案:

答案 0 :(得分:5)

尝试在DAO中执行:

public void delete(Person person){
        currentSession().delete(person);
    }

答案 1 :(得分:1)

如何使用@DELETE方法?

@DELETE
@Path("/{id}")
public void delete(@PathParam("id") IntParam id) {
  peopleDAO.deleteById(id.get());
}