我们如何使用dropwizard web serivce实现删除操作

Meh*_*hat 1 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
}
Run Code Online (Sandbox Code Playgroud)

这是我的服务文件:

@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
}
Run Code Online (Sandbox Code Playgroud)

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

gon*_*lla 6

尝试在DAO中做:

public void delete(Person person){
        currentSession().delete(person);
    }
Run Code Online (Sandbox Code Playgroud)