Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we implement delete operation using dropwizard web serivce

I read many blog and tutorial, but i didn't find delete operation in dropwizard web services.

Here is my code:

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
}

here is my service file:

@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
}

If anyone have an idea please suggest me, thanks in advance.

like image 834
Mehul Dudhat Avatar asked Jul 16 '26 08:07

Mehul Dudhat


2 Answers

Try to do in DAO:

public void delete(Person person){
        currentSession().delete(person);
    }
like image 133
gonella Avatar answered Jul 17 '26 21:07

gonella


How about using a @DELETE method?

@DELETE
@Path("/{id}")
public void delete(@PathParam("id") IntParam id) {
  peopleDAO.deleteById(id.get());
}
like image 32
condit Avatar answered Jul 17 '26 21:07

condit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!